Linux Socket编程实例(一个Hello World程序)

在Linux下写了个小的socket程序,分为客户端和服务器端,服务端开一个端口(2000),做为一个daemon,等待客户的连接请求.一旦有客户连接,服务器端打印出客户端的IP地址和端口,并且向服务器端发送欢迎信息和时间.下面是服务端的代码(tcpserver.c).由于这只是个简单的程序,所以只用了单线程实现!

ExpandedBlockStart.gif /* *
InBlock.gif * Tcp Server program, It is a simple example only.
InBlock.gif * zhengsh 200520602061 2
InBlock.gif * when client connect to server, send a welcome message and timestamp in server.
ExpandedBlockEnd.gif  
*/ 
None.gif
None.gif#include  <stdio.h> 
None.gif#include  <sys/socket.h> 
None.gif#include  <unistd.h> 
None.gif#include  <sys/types.h> 
None.gif#include  <netinet/ in.h> 
None.gif#include  <stdlib.h> 
None.gif#include  <time.h> 
None.gif 
None.gif #define  SERVER_PORT 20000   //   define the defualt connect port id 
None.gif #define  LENGTH_OF_LISTEN_QUEUE 10   //  length of listen queue in server 
None.gif #define  BUFFER_SIZE 255 
None.gif #define  WELCOME_MESSAGE "welcome to connect the server. " 
None.gif
None.gif int main( int argc,  char** argv)
ExpandedBlockStart.gif {
InBlock.gif       int  servfd,clifd;
InBlock.gif       struct  sockaddr_in servaddr,cliaddr;
InBlock.gif
InBlock.gif       if  ((servfd  =  socket(AF_INET,SOCK_STREAM, 0 ))  <   0 )
ExpandedSubBlockStart.gif         {
InBlock.gif              printf( " create socket error!\n " );
InBlock.gif              exit( 1 );
ExpandedSubBlockEnd.gif       } 
InBlock.gif 
InBlock.gif       bzero( & servaddr, sizeof (servaddr));
InBlock.gif
InBlock.gif       servaddr.sin_family  =  AF_INET;
InBlock.gif       servaddr.sin_port  =  htons(SERVER_PORT);
InBlock.gif       servaddr.sin_addr.s_addr  =  htons(INADDR_ANY);
InBlock.gif
InBlock.gif       if  (bind(servfd,( struct  sockaddr * ) & servaddr, sizeof (servaddr)) < 0 )
ExpandedSubBlockStart.gif         {
InBlock.gif              printf( " bind to port %d failure!\n " ,SERVER_PORT);
InBlock.gif              exit( 1 );
ExpandedSubBlockEnd.gif       } 
InBlock.gif 
InBlock.gif        if  (listen(servfd,LENGTH_OF_LISTEN_QUEUE)  <   0 )
ExpandedSubBlockStart.gif         {
InBlock.gif              printf( " call listen failure!\n " );
InBlock.gif              exit( 1 );
ExpandedSubBlockEnd.gif       } 
InBlock.gif 
InBlock.gif        while  ( 1 )
ExpandedSubBlockStart.gif         { // server loop will nerver exit unless any body kill the process 
InBlock.gif

InBlock.gif              char  buf[BUFFER_SIZE];
InBlock.gif              long  timestamp;
InBlock.gif              socklen_t length  =   sizeof (cliaddr);
InBlock.gif              clifd  =  accept(servfd,( struct  sockaddr * ) & cliaddr, & length);
InBlock.gif
InBlock.gif               if  (clifd  <   0 )
ExpandedSubBlockStart.gif                {
InBlock.gif                     printf( " error comes when call accept!\n " );
InBlock.gif                     break ;
ExpandedSubBlockEnd.gif              } 
InBlock.gif 
InBlock.gif              strcpy(buf,WELCOME_MESSAGE);
InBlock.gif
InBlock.gif               // inet_ntop(INET_ADDRSTRLEN,cliaddr.sin_addr,buf,BUFFER_SIZE); 
InBlock.gif

InBlock.gif              printf( " from client,IP:%s,Port:%d\n " ,inet_ntoa(cliaddr.sin_addr),ntohs(cliaddr.sin_port));
InBlock.gif
InBlock.gif              timestamp  =  time(NULL);
InBlock.gif
InBlock.gif              strcat(buf, " timestamp in server: " );
InBlock.gif              strcat(buf,ctime( & timestamp));
InBlock.gif
InBlock.gif              send(clifd,buf,BUFFER_SIZE, 0 );
InBlock.gif
InBlock.gif              close(clifd);           
InBlock.gif
ExpandedSubBlockEnd.gif       } // exit 
InBlock.gif
 
InBlock.gif       close(servfd);
InBlock.gif
InBlock.gif        return   0 ;
ExpandedBlockEnd.gif}


 客户每次用一个随机的端口连接服务器,并接收来自服务器的欢迎信息,然后打印出来(tcpclient).运行的时候接受一个参数,也就是服务器的ip地址.
ExpandedBlockStart.gif /*  Tcp client program, It is a simple example only.
InBlock.gif * zhengsh 200520602061 2
InBlock.gif * connect to server, and echo a message from server.
ExpandedBlockEnd.gif
*/ 
None.gif
None.gif
None.gif#include <stdio.h>
None.gif#include <sys/socket.h>
None.gif#include <unistd.h>
None.gif#include <sys/types.h>
None.gif#include <netinet/ in.h>
None.gif#include <stdlib.h> 
None.gif
None.gif
None.gif #define  SERVER_PORT 20000   //   define the defualt connect port id 
None.gif #define  CLIENT_PORT ((20001+rand())%65536)   //   define the defualt client port as a random port 
None.gif #define  BUFFER_SIZE 255 
None.gif #define  REUQEST_MESSAGE "welcome to connect the server.\n" 
None.gif
None.gif
None.gif void  usage( char* name)
ExpandedBlockStart.gif {
InBlock.gif       printf( " usage: %s IpAddr\n " ,name);
ExpandedBlockEnd.gif}
None.gif
None.gif
None.gif int  main( int argc,  char** argv)
ExpandedBlockStart.gif {
InBlock.gif       int  servfd,clifd,length = 0;
InBlock.gif       struct  sockaddr_in servaddr,cliaddr;
InBlock.gif       socklen_t socklen  =   sizeof (servaddr);
InBlock.gif       char  buf[BUFFER_SIZE];
InBlock.gif
InBlock.gif        if (argc < 2 )
ExpandedSubBlockStart.gif         {
InBlock.gif              usage(argv[ 0 ]);
InBlock.gif              exit( 1 );
ExpandedSubBlockEnd.gif       } 
InBlock.gif
InBlock.gif       if ((clifd  =  socket(AF_INET,SOCK_STREAM, 0 ))  <   0 )
ExpandedSubBlockStart.gif         {
InBlock.gif             printf( " create socket error!\n " );
InBlock.gif             exit( 1 );
ExpandedSubBlockEnd.gif       } 
InBlock.gif 
InBlock.gif       srand(time(NULL)); // initialize random generator 
InBlock.gif
 
InBlock.gif       bzero( & cliaddr, sizeof (cliaddr));
InBlock.gif       cliaddr.sin_family  =  AF_INET;
InBlock.gif       cliaddr.sin_port  =  htons(CLIENT_PORT);
InBlock.gif       cliaddr.sin_addr.s_addr  =  htons(INADDR_ANY);
InBlock.gif
InBlock.gif       bzero( & servaddr, sizeof (servaddr));
InBlock.gif       servaddr.sin_family  =  AF_INET;
InBlock.gif       inet_aton(argv[ 1 ], & servaddr.sin_addr);
InBlock.gif       servaddr.sin_port  =  htons(SERVER_PORT);
InBlock.gif      // servaddr.sin_addr.s_addr = htons(INADDR_ANY); 
InBlock.gif

InBlock.gif       if  (bind(clifd, (struct sockaddr* ) &cliaddr, sizeof (cliaddr)) < 0 )
ExpandedSubBlockStart.gif       {
InBlock.gif              printf( " bind to port %d failure!\n " ,CLIENT_PORT);
InBlock.gif              exit( 1 );
ExpandedSubBlockEnd.gif       } 
InBlock.gif
InBlock.gif        if (connect(clifd,( struct  sockaddr * ) & servaddr, socklen)  <   0 )
ExpandedSubBlockStart.gif       {
InBlock.gif              printf( " can't connect to %s!\n ", argv[ 1 ]);
InBlock.gif              exit( 1 );
ExpandedSubBlockEnd.gif       } 
InBlock.gif
InBlock.gif       length  =  recv(clifd, buf, BUFFER_SIZE, 0);
InBlock.gif        if  (length < 0)
ExpandedSubBlockStart.gif        {
InBlock.gif              printf( " error comes when recieve data from server %s! ", argv[1] );
InBlock.gif              exit( 1 );
ExpandedSubBlockEnd.gif       } 
InBlock.gif
InBlock.gif       printf( " from server %s :\n\t%s", argv[1], buf);
InBlock.gif
InBlock.gif       close(clifd);
InBlock.gif       return 0;
ExpandedBlockEnd.gif
None.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值