Socket Programming- TCP client

2 01 2008

This a very basic tutorial for TCP server-client.

The code against tcp-server , the Clients Code

#define SRVR_PORT 4030 // The Server’s Port which Client is going to Connect.

#define MAXBUFLEN 100// Buffer Size for Sending/Receiving

int main(int argc, char *argv[])

int sockfd;
struct sockaddr_in their_addr; // connector’s address information
struct hostent *he;
int numbytes;
char buf[MAXBUFLEN];

//The Code to Take Server’s Address at Commandline

//You can remove this and assign your server’s Address you want

if (argc != 2)
fprintf(stderr,”usage: ./client\n”);

if ((he=gethostbyname(argv[1])) == NULL) { // get the host info

//Creating the Socket(The Door for Communication)

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {

//Setting the Attributes for Server Address

their_addr.sin_family = AF_INET; // host byte order
their_addr.sin_port = htons(SRVR_PORT); // short, network byte order
their_addr.sin_addr.s_addr = inet_addr(“″);//*((struct in_addr *)he->h_addr);
memset(&(their_addr.sin_zero),”,8); // zero the rest

int addrlen = sizeof(struct sockaddr);

//Connecting to The Server

//connect return the status for successful connection or failure

int status;
status = connect(sockfd,(struct sockaddr*)&their_addr,addrlen);


//Taking Input msg from Console

//Remove this if you not want from console Then use strcpy to fill the buffer

cout << “Enter ur message: “;

//The buffer conatain the message you want to send to server

// The send method sends to the sevrer , just give socket id , buffer , and buffer size to send

// and off course give error on failure

if ( (numbytes=send(sockfd, buf, strlen(buf) , 0)) == -1)

// To Nullify or Empty to the buffer use memset method


// The recv method is used for tcp recive , just sock id, buffer in which recv and length

// method return non-positive value for error

if ((numbytes=recv((int)sockfd,buf, MAXBUFLEN-1, 0)) == -1)

cout << “from server>” << buf << endl;


printf(“sent %d bytes to %s\n”, numbytes,inet_ntoa(their_addr.sin_addr));

close(sockfd); // It’s good to close the socket(the door) after Communication

return 0;


Let me know if you not uderstand any thing. I will upload the complete source code.


Insertion Sort Algorithm

11 12 2007

he Insertion sort works just like its name suggests – it inserts each item into its proper place in the final list. The simplest implementation of this requires two list structures – the source list and the list into which sorted items are inserted. To save memory, most implementations use an in-place sort that works by moving the current item past the already sorted items and repeatedly swapping it with the preceding item until it is in place.The Insertion sort is a little over twice as efficient as the bubble sort.

The insertion sort is a good middle-of-the-road choice for sorting lists of a few thousand items or less. The algorithm is significantly simpler than the shell sort, with only a small trade-off in efficiency. At the same time, the insertion sort is over twice as fast as the bubble sort and almost 40% faster than the selection sort. The insertion sort shouldn’t be used for sorting lists larger than a couple thousand items or repetitive sorting of lists larger than a couple hundred items.

Insertion Sort Routine

// array of integers to hold values
private int[] a = new int[100];

// number of elements in array
private int x;

// Insertion Sort Algorithm
public void sortArray()
int i;
int j;
int index;

for( i = 1; i < x; i++ )
index = a[i];
j = i;

while( (j > 0) && (a[j-1] > index) )
a[j] = a[j-1];
j = j – 1;

a[j] = index;

You can download the full code here, or the compiled code here.

further :

Microsoft in Pakistan

10 12 2007

Contact at:

Microsoft Corporation
Pakistan Liaison Office
2A, Ground Floor
Evacuee Trust Building
Agha Khan Raod, F-5/1
UAN: (92-51) 111116738
Fax: (92-51) 2872620

The Forum, Suite # 202

G-20, Block – 9, Khayaban-e-Jami – Clifton , Karachi , 75600

Tel: +92-21-5369101 – 3
Fax: +92-21-5369106

UAN: +92-21-111 11 MSFT

Technical Support: +92-21-111 MS HELP

Imagine Cup in Pakistan

10 12 2007

The Olympics for Programmers   

“Imagine Cup is an annual competition sponsored and hosted by Microsoft Corp. which brings together young technologists worldwide to help resolve some of the world’s toughest challenges.”

This year, Microsoft is calling on young programmers, artists and technologists around the world to “imagine a world where technology enables a sustainable environment.”

A number of Competition Categories for every one. Now every one can participate no excuses….!

Competition Categories includes

  • Software Design (for team. bIg!)
  • Game Development (for good logicians, artists)
  • IT Challenge( for single : to develop, deploy, and maintain IT systems)
  • Algorithm( for Hardcore CS students)
  • Photography(for every one? Yeah, i am not joking)
  • Short Film(for every one)
  • Interface Design(for girls han? no for all who have some good aesthetic,artistic,imaginative(too much 🙂 ) sense)

and ……. many other.

for detail just go Its Fun.!