Client:
#include <stdio.h> #include <sys/socket.h> #include <unistd.h> #include <sys/types.h&
Gt #include <netinet/in.h> #include <stdlib.h> #define SERVER_PORT 20000//define the Defualt con
Nect Port ID #define CLIENT_PORT ((20001+rand ())%65536)//define the Defualt CLIENT port as a random port #define BUFFER_SIZE 255 #define REUQEST_MESSAGE "Welcome to connect the server.\n" void
Usage (char* name) {printf ("Usage:%s ipaddr\n", name);
int main (int argc, char** argv) {int servfd,clifd,length = 0;
struct sockaddr_in servaddr,cliaddr;
socklen_t Socklen = sizeof (SERVADDR);
Char Buf[buffer_size];
if (ARGC < 2) {usage (argv[0]);
Exit (1); } if ((CLIFD = socket (af_inet,sock_stream, 0)) < 0) {printf ("Create socket error!\n");
Exit (1); } Srand (Time (NULL));
Initialize Random generator bzero (& cliaddr, sizeof (CLIADDR));
cliaddr.sin_family = af_inet;
Cliaddr.sin_port = htons (Client_port);
CLIADDR.SIN_ADDR.S_ADDR = htons (Inaddr_any);
Bzero (& servaddr, sizeof (SERVADDR));
servaddr.sin_family = af_inet;
Inet_aton (argv[1], & SERVADDR.SIN_ADDR);
Servaddr.sin_port = htons (Server_port);
SERVADDR.SIN_ADDR.S_ADDR = htons (Inaddr_any); if (Bind (CLIFD, (struct sockaddr*) &cliaddr, sizeof (CLIADDR)) < 0) {printf ("b
IND to port%d failure!\n ", client_port);
Exit (1); } if (Connect (CLIFD, (struct sockaddr *) & Servaddr, Socklen) < 0)
{printf ("can ' t connect to%s!\n", argv[1]);
Exit (1);
Length = recv (CLIFD, buf, buffer_size, 0); if (length < 0) {printf ("error comes when recieve data from server%s!", argv[1])
;
Exit (1);
printf ("From server%s: \n\t%s", argv[1], buf);
Close (CLIFD);
return 0; }
Server-side:
#include <stdio.h> #include <sys/socket.h> #include <unistd.h> #include <sys/t
ypes.h> #include <netinet/in.h> #include <stdlib.h> #include <time.h> #define SERVER_PORT 20000//define the Defualt connect PORT ID #define LENGTH_OF_LISTEN_QUEUE/len Gth of Listen queue in server #define BUFFER_SIZE 255 #define Welcome_message ' WELCOME to connect the server .
int main (int argc, char** argv) {int servfd,clifd;
struct sockaddr_in servaddr,cliaddr; if ((servfd = socket (af_inet,sock_stream, 0)) < 0) {printf ("Create socket E
Rror!\n ");
Exit (1);
Bzero (& servaddr, sizeof (SERVADDR));
servaddr.sin_family = af_inet; Servaddr.sin_port = htons (server_poRT);
SERVADDR.SIN_ADDR.S_ADDR = htons (Inaddr_any); if (bind (servfd, struct sockaddr *) & servaddr, sizeof (SERVADDR)) < 0) {pri
NTF ("Bind to Port%d failure!\n", server_port);
Exit (1); } if (Listen (Servfd,length_of_listen_queue) < 0) {print
F ("Call listen failure!\n");
Exit (1);
while (1) {//server loop would nerver exit unless any body kill the process
Char Buf[buffer_size];
Long timestamp;
socklen_t length = sizeof (CLIADDR);
CLIFD = Accept (servfd, struct sockaddr *) & cliaddr, & length); if (Clifd < 0) {printf ("error comes when call accept!\n");
break;
} strcpy (Buf,welcome_message);
Inet_ntop (inet_addrstrlen,cliaddr.sin_addr,buf,buffer_size);
printf ("From client,ip:%s,port:%d\n", Inet_ntoa (CLIADDR.SIN_ADDR), Ntohs (Cliaddr.sin_port));
Timestamp = time (NULL);
strcat (buf, "timestamp in server:");
Strcat (Buf,ctime (& timestamp));
Send (clifd,buf,buffer_size, 0);
Close (CLIFD);
}//Exit Close (SERVFD);
return 0; }
View a full set of articles: Http://www.bianceng.cn/Programming/C/201212/34807.htm