Chinaunix首页 | 论坛 | 博客
  • 博客访问: 64227
  • 博文数量: 21
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 236
  • 用 户 组: 普通用户
  • 注册时间: 2014-09-05 21:34
文章分类

全部博文(21)

文章存档

2015年(21)

我的朋友

分类: LINUX

2015-07-30 12:52:54

昨天弄了sublime之后没有弄输入中文的了,学生党来着,刚好可以练练英语(英语渣渣,还要考六级),所以注释都写英文的(语法什么的就别太深究了
服务器端:

点击(lcw)折叠或打开

  1. /*start from the very beginning,and create greatness
  2. @name:Chuangwei Lin
  3. @E-mail:979951191@qq.com
  4. @brief:a simple example about server/client link(server c file)
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <strings.h>
  9. #include <sys/types.h>
  10. #include <sys/socket.h>
  11. #include <unistd.h>
  12. #include <linux/in.h>
  13. #define PORT 8888 //port to listen
  14. #define BACKLOG 2 //the length of listen queue
  15. void process_conn_server(int s);
  16. int main(int argc, char const *argv[])
  17. {
  18.     int ss,sc; //ss:server's socket file descriptor;sc:client's socket file descriptor
  19.     struct sockaddr_in server_addr; //server's address struct
  20.     struct sockaddr_in client_addr; //use to save client's address
  21.     int err; //the return value
  22.     pid_t pid; //the process ID
  23.     //create a socket
  24.     ss = socket(AF_INET,SOCK_STREAM,0);
  25.     if (ss < 0) //error
  26.     {
  27.         printf("socket error\n");
  28.         return -1;
  29.     }
  30.     //set the server address
  31.     bzero(&server_addr,sizeof(server_addr)); //clear
  32.     server_addr.sin_family = AF_INET; //protocol family
  33.     server_addr.sin_addr.s_addr = htonl(INADDR_ANY);//local address
  34.     server_addr.sin_port = htons(PORT); //server port
  35.     //binging
  36.     err = bind(ss,(struct sockaddr*)&server_addr,sizeof(server_addr));
  37.     if (err < 0)
  38.     {
  39.         printf("bind error\n");
  40.         return -1;
  41.     }
  42.     //listening
  43.     err = listen(ss,BACKLOG);
  44.     if (err < 0)
  45.     {
  46.         printf("listen error\n");
  47.         return -1;
  48.     }
  49.     //the main loop
  50.     for (;;)
  51.     {
  52.         int addrlen = sizeof (struct sockaddr);
  53.         sc = accept(ss,(struct sockaddr*)&client_addr,&addrlen); //accept the client's connecting
  54.         if (sc < 0)
  55.         {
  56.             continue; //if error,finish this time loop
  57.         }
  58.         //create a new process to process the connecting
  59.         pid = fork();
  60.         if (0 == pid)
  61.         {
  62.             close(ss); //close listen in the child process
  63.             process_conn_server(sc); // process
  64.         }
  65.         else
  66.         {
  67.             close(sc); //close accept in the parent process
  68.         }

  69.     }

  70.     return 0;
  71. }
  72. //the process function
  73. void process_conn_server(int s)
  74. {
  75.     ssize_t size = 0 ;
  76.     char buffer[1024]; //data buffer
  77.     for(;;)
  78.     {
  79.         size = read(s,buffer,1024); //read data from socket and put in buffer
  80.         if (0 ==size) //if no data
  81.         {
  82.             return; //return
  83.         }
  84.         sprintf(buffer,"%d bytes altogether\n",size); //send the number of data return
  85.         write(s,buffer,strlen(buffer)+1);
  86.     }

  87. }
客户端:

点击(lcw)折叠或打开

  1. /*start from the very beginning,and create greatness
  2. @name:Chuangwei Lin
  3. @E-mail:979951191@qq.com
  4. @brief:a simple example about server/client link(client c file)
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <strings.h>
  9. #include <sys/types.h>
  10. #include <sys/socket.h>
  11. #include <unistd.h>
  12. #include <linux/in.h>
  13. #define PORT 8888 //port to listen
  14. void process_conn_client(int s);
  15. int main(int argc, char const *argv[])
  16. {
  17.     int s; //s:socket descriptor
  18.     struct sockaddr_in server_addr; // server address
  19.     int err;
  20.     //create a socket
  21.     s = socket(AF_INET,SOCK_STREAM,0);
  22.     if (s < 0)
  23.     {
  24.         printf("socket error\n");
  25.         return -1;
  26.     }
  27.     //set the server address
  28.     bzero(&server_addr,sizeof(server_addr));
  29.     server_addr.sin_family = AF_INET; //protocol family
  30.     server_addr.sin_addr.s_addr = htonl(INADDR_ANY); //local address
  31.     server_addr.sin_port = htons(PORT); //server port

  32.     //change the user input char IP to int
  33.     inet_pton(AF_INET,argv[1],&server_addr.sin_addr);
  34.     //conneting the server
  35.     connect(s,(struct sockaddr*)&server_addr,sizeof(struct sockaddr));
  36.     process_conn_client(s);
  37.     close(s);
  38.     return 0;
  39. }
  40. void process_conn_client(int s)
  41. {
  42.     ssize_t size = 0 ;
  43.     char buffer[1024]; //data buffer
  44.     for(;;)
  45.     {
  46.         //get data from standard input
  47.         size = read (0,buffer,1024);
  48.         if(size > 0)
  49.         {
  50.             write(s,buffer,size); //send to server
  51.             size = read(s,buffer,1024); //read from server
  52.             write(1,buffer,size); //write to standard output
  53.         }
  54.     }

  55. }
很简单的一段,只是建立连接然后客户端发送字符然后服务器统计后返回



阅读(1203) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~