Socket programming tutorial

Wednesday, September 8, 2010

Generic Socket Programming tutorial

S.Prasanna
sprasanna199@gmail.com

This is a generic socket programming tutorial which mainly concentrates on how to communicate with two machines using TCP/IP and UDP/IP protocols irrespective of the programming language used to implement the server and client

I will list a simple Connection oriented (TCP) and Connectionless (UDP) Server and Client programs for all the four languages namely Python , Perl, C and Java. The TCP Server program is an iterative one which means it will process clients one by one. The communication between the TCP Server and the Client is a simple chat program. If the client quits, the current TCP connection between the server and the client gets disconnected and the server processes the next client and if the server decides to quit, that client gets disconnected and the server processes the next client.

The UDP Server program is very simple. The server recieves datagrams from many clients and prints them.

In all the programs , the Server process listens at port 5000 and I have used loopback address for the Server so that you can run the server and the client in different terminals in the same system. Besides you can use any of the four servers implemented in Python , Perl, C and Java with any of the four client programs implemented inthe four languages.In short you can use a Client program in Perl with a Server program in python , Server program in python with a Client progam in C , etc. Morever you can invoke the client program without any command line arguments since the client and the server are designed to communicate using the loopback address.

I will go from easy to difficult ( in terms of readability and lines of code ). i.e for example socket programming is simple and easy to understand in Python than in C or java.

1. Socket programming in Python

Connection oriented Server and Client program.

tcpserver.py and tcpclient.py

Connectionless Server and Client program.

udpserver.py and udpclient.py


2. Socket programming in Perl

Connection oriented Server and Client program.

tcpserver.pl and tcpclient.pl

Connectionless Server and Client program.

udpserver.pl and udpclient.pl


3. Socket programming in C

Connection oriented Server and Client program.

tcpserver.c and tcpclient.c

Connectionless Server and Client program.

udpserver.c and udpclient.c


4. Socket programming in Java

Connection oriented Server and Client program.

TCPServer.java and TCPClient.java

Connectionless Server and Client program.

UDPServer.java and UDPClient.java

I will list similar programs in as many languages in future. I could have explained about the details of sockets, how system calls like socket , bind, listen, etc works, but those should be explained from the perspectives different languages which is beyond the scope of this tutorial. In general these programs explain the fundamental concepts of socket programming irrespective of their implementation language.

Socket programming can be easily understood in interpreted languages like Perl and Python. Perl, especially is weapon for hackers. Dangerous scripts can be developed in minutes, in perl. For example spoofing source IP address can easliy be done in Perl but it requires some effort in C or in other languages. This small script shows how to spoof UDP source address.

Raw Sockets Programming (UDP Spoofing):

udpspoof.pl and udpserver.c

When you run the above simple script as client you can see the fake IP source address and the port the client used at the UDPServer. To execute the Perl script you need the Net::RawIP Module which can be downloaded here and also its documentation.

TCP/IP spoofing is a challenging task and involves a lot of calculation involving prediction of sequence numbers,etc but worth giving a try.

References:

1. Beej's guide to Network Programming

The highly rated Socket Programming tutorial in the Web. It explains all system calls used for socket programming in a clear way and also about Networking concepts in general.

2. UNIX Sockets for Newbies

Another Wonderful Socket programming tutorial.

3. Network Programming in Python

An excellent document on Python Socket programming.

4. UNIX Sockets FAQ

Answers to common questions asked by socket programmers.

5. Perl Socket Programming tutorial

Good introduction to Perl sockets.

6. http://pont.net/socket/index.html

A Collection of different Client-Server programs in C and Java.

4 comments:

Diya shree said...

Thanks for your article.It is very easy to understand and Great efforts put it in this site.Thanks for sharing this post.
Java training in chennai

Ana said...

I can not oppen the files
can some one plz help me

langfordoberhaus said...

LuckyClub Casino Site - 2021 Review & Rating
Lucky Club casino luckyclub site review and list of all bonuses, customer support, banking and bonuses. Lucky Club Casino site was launched  Rating: 4.3 · ‎Review by LuckyClub

nafunalaakso said...

The Top 5 Casino With Slots: Play Real Money Online
The Top 보령 출장마사지 5 거제 출장마사지 Casino With Slots: Play 남양주 출장샵 Real Money Online · 1. BitStarz Casino – Best Overall Casino Site · 2. Royal Panda Casino – Highest Paying Casino 태백 출장샵 · 3. 성남 출장안마 mBit

Post a Comment