QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 2656|回复: 0

一个 C 实现最基本的 socket 编程例子

[复制链接]
发表于 2009-5-20 18:25:47 | 显示全部楼层 |阅读模式
服务端 service.c
  1. #include <stdio.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <arpa/inet.h>
  5. #include <netdb.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. int port = 17000;
  9. main ()
  10. {
  11.   struct sockaddr_in sin;
  12.   struct sockaddr_in pin;
  13.   int sock_descriptor;
  14.   int temp_sock_descriptor;
  15.   int size_of_addr;
  16.   size_of_addr=sizeof(pin);
  17.   char buf[8192];
  18.   int i, lenth;
  19.   sock_descriptor = socket (AF_INET, SOCK_STREAM, 0);
  20.   bzero (&sin, sizeof (sin));
  21.   sin.sin_family = AF_INET;
  22.   sin.sin_addr.s_addr = htonl(INADDR_ANY);
  23.   sin.sin_port = htons (port);
  24.   if (bind (sock_descriptor, (struct sockaddr *) &sin, sizeof (sin)) == -1)
  25.       {
  26.       perror ("bind!"); exit (1);}
  27.       if (listen (sock_descriptor, 20) == -1)
  28.       {
  29.       perror ("listen!"); exit (1);}
  30.       printf ("waiting for accepting connections from client\n");
  31. while (1)
  32.       {
  33.         temp_sock_descriptor = accept(sock_descriptor,(struct sockaddr *)&pin,&size_of_addr);
  34.         if (temp_sock_descriptor == -1)
  35.       {
  36.       perror ("call to accept"); }
  37.       if (recv (temp_sock_descriptor, buf, sizeof (buf), 0) == -1)
  38.       {
  39.       perror ("recv!"); exit (1);}
  40.       printf ("received:%s\n", buf);
  41.       lenth = strlen (buf);
  42. for (i = 0; i <= lenth; i++)
  43.       {
  44.       buf[i] = toupper (buf[i]);
  45.       if (send (temp_sock_descriptor, buf, lenth, 0) == -1)
  46.       {
  47.       perror ("send!"); exit (1);}
  48.       }
  49. close (temp_sock_descriptor);
  50.       }
  51. }
复制代码
客户端 client.c
  1. #include <stdio.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <arpa/inet.h>
  5. #include <netdb.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. char * host_name="127.0.0.1";
  9. int port=17000;
  10. void main(int argc,char *argv[])
  11. {
  12. char buf[4096];
  13. char message[1024];
  14. int socket_descriptor;
  15. struct sockaddr_in pin;
  16. struct hostent *server_host_name;
  17. char *message_str="hello,network";
  18. if(argc<2)
  19. {printf("send default srting.\n");
  20. }else{
  21. message_str=argv[1];
  22. }
  23. if((server_host_name=gethostbyname(host_name))==0)
  24. {
  25. perror("cant resolving localhost\n");
  26. exit(1);
  27. }
  28. bzero(&pin,sizeof(pin));
  29. pin.sin_family=AF_INET;
  30. pin.sin_addr.s_addr=htonl(INADDR_ANY);
  31. pin.sin_addr.s_addr=((struct in_addr *)(server_host_name->h_addr))->s_addr;
  32. pin.sin_port=htons(port);
  33. if((socket_descriptor=socket(AF_INET,SOCK_STREAM,0))==-1)
  34. {perror("Error opening socket\n");
  35. exit(1);
  36. }
  37. if(connect(socket_descriptor,(void *)&pin,sizeof(pin))==-1)
  38. {
  39. perror("cant connecting to server");
  40. exit(1);
  41. }
  42. printf("send message %s to server ...\n",message_str);
  43. if (send(socket_descriptor,message_str,strlen(message_str),0)==-1)
  44. {
  45. perror("cant send message\n");
  46. exit(1);
  47. }
  48. printf("waiting for response from server\n");
  49. if (recv(socket_descriptor,buf,sizeof(buf),0)==-1)
  50. {
  51. perror("cant receive response\n");
  52. exit(1);
  53. }
  54. printf("\nResponse from server:\n\n%s\n",buf);
  55. close(socket_descriptor);
  56. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-3-29 08:12 , Processed in 0.053892 second(s), 15 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表