Chinaunix首页 | 论坛 | 博客
  • 博客访问: 39317
  • 博文数量: 6
  • 博客积分: 125
  • 博客等级: 入伍新兵
  • 技术积分: 65
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-10 23:27
文章分类

全部博文(6)

文章存档

2012年(6)

最近访客

分类: LINUX

2012-06-10 23:07:29

TCP编程服务器端一般步骤: 1、创建一个socket,用函数socket(); 2、设置socket属性,用函数setsockopt(); * 可选 3、绑定IP地址、端口等信息到socket上,用函数bind(); 4、开启监听,用函数listen(); 5、接收客户端上来的连接,用函数accept(); 6、收发数据,用函数send()和recv(),或者read()和write(); 7、关闭网络连接; 8、关闭监听; TCP编程客服端一般步骤: 1、创建一个socket,用函数socket(); 2、设置socket属性,用函数setsockopt();* 可选 3、绑定IP地址、端口等信息到socket上,用函数bind();* 可选 4、设置要连接的对方的IP地址和端口等属性; 5、连接服务器,用函数connect(); 6、收发数据,用函数send()和recv(),或者read()和write(); 7、关闭网络连接; [c-sharp] view plaincopyprint? 01.TCP服务器端tcpserver.c 02.#include 03.#include 04.#include 05.#include 06.#include 07.#include 08. 09.#define BUFSIZE 1024 10. 11.int main(int argc,char **argv) 12.{ 13. int server_sockfd,client_sockfd; 14. int len; 15. struct sockaddr_in server_addr,client_addr; 16. int sin_size; 17. char buf[BUFSIZE]; 18. 19. memset(&server_addr,0,sizeof(server_addr)); 20. server_addr.sin_family = AF_INET; 21. server_addr.sin_port = htons(8000); 22. server_addr.sin_addr.s_addr = INADDR_ANY; 23. 24. if((server_sockfd = socket(PF_INET,SOCK_STREAM,0))<0) 25. { 26. perror("socket"); 27. return 1; 28. } 29. 30. if(bind(server_sockfd,(struct sockaddr *)&server_addr,sizeof(struct sockaddr))<0) 31. { 32. perror("bind"); 33. return 1; 34. } 35. 36. listen(server_sockfd,5); 37. 38. sin_size = sizeof(struct sockaddr_in); 39. 40. if((client_sockfd = accept(server_sockfd,(struct sockaddr *)&client_addr,&sin_size))<0) 41. { 42. perror("accept"); 43. return 1; 44. } 45. printf("accept client is %s/n",inet_ntoa(client_addr.sin_addr)); 46. 47. len = send(client_sockfd,"Welcome to zechin server/n",25,0); 48. 49. while(1) 50. { 51. len = recv(client_sockfd,buf,BUFSIZE,0); 52. if(len < 0) 53. printf("Did not receive any message/n"); 54. else 55. { 56. if(!strcmp(buf,"end")) 57. break; 58. buf[len]='/0'; 59. printf("Receive message:%s/n",buf); 60. } 61. 62. if(send(client_sockfd,buf,len,0)<0) 63. printf("send failed/n"); 64. } 65. 66. close(client_sockfd); 67. close(server_sockfd); 68. return 0; 69.} TCP服务器端tcpserver.c #include #include #include #include #include #include #define BUFSIZE 1024 int main(int argc,char **argv) { int server_sockfd,client_sockfd; int len; struct sockaddr_in server_addr,client_addr; int sin_size; char buf[BUFSIZE]; memset(&server_addr,0,sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(8000); server_addr.sin_addr.s_addr = INADDR_ANY; if((server_sockfd = socket(PF_INET,SOCK_STREAM,0))<0) { perror("socket"); return 1; } if(bind(server_sockfd,(struct sockaddr *)&server_addr,sizeof(struct sockaddr))<0) { perror("bind"); return 1; } listen(server_sockfd,5); sin_size = sizeof(struct sockaddr_in); if((client_sockfd = accept(server_sockfd,(struct sockaddr *)&client_addr,&sin_size))<0) { perror("accept"); return 1; } printf("accept client is %s/n",inet_ntoa(client_addr.sin_addr)); len = send(client_sockfd,"Welcome to zechin server/n",25,0); while(1) { len = recv(client_sockfd,buf,BUFSIZE,0); if(len < 0) printf("Did not receive any message/n"); else { if(!strcmp(buf,"end")) break; buf[len]='/0'; printf("Receive message:%s/n",buf); } if(send(client_sockfd,buf,len,0)<0) printf("send failed/n"); } close(client_sockfd); close(server_sockfd); return 0; } [c-sharp] view plaincopyprint? 01.TCP客服端tcpclient.c 02.#include 03.#include 04.#include 05.#include 06.#include 07.#include 08. 09.#define BUFSIZE 1024 10. 11.int main(int argc,char **argv) 12.{ 13. int client_sockfd; 14. int len; 15. struct sockaddr_in server_addr; 16. char buf[BUFSIZE]; 17. 18. memset(&server_addr,0,sizeof(server_addr)); 19. server_addr.sin_family = AF_INET; 20. server_addr.sin_port = htons(8000); 21. server_addr.sin_addr.s_addr = inet_addr("192.168.1.8"); 22. 23. if((client_sockfd = socket(PF_INET,SOCK_STREAM,0))<0) 24. { 25. perror("socket"); 26. return 1; 27. } 28. 29. if(connect(client_sockfd,(struct sockaddr *)&server_addr,sizeof(struct sockaddr))<0) 30. { 31. perror("connect"); 32. return 1; 33. } 34. 35. len = recv(client_sockfd,buf,BUFSIZE,0); 36. buf[len] = '/0'; 37. printf("%s/n",buf); 38. 39. while(1) 40. { 41. printf("Entet string to send:"); 42. scanf("%s",buf); 43. if(!strcmp(buf,"quit")) 44. break; 45. len = send(client_sockfd,buf,strlen(buf),0); 46. len = recv(client_sockfd,buf,BUFSIZE,0); 47. buf[len]='/0'; 48. printf("Receive:%s/n",buf); 49. } 50. close(client_sockfd); 51. return 0; 52.} TCP客服端tcpclient.c #include #include #include #include #include #include #define BUFSIZE 1024 int main(int argc,char **argv) { int client_sockfd; int len; struct sockaddr_in server_addr; char buf[BUFSIZE]; memset(&server_addr,0,sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(8000); server_addr.sin_addr.s_addr = inet_addr("192.168.1.8"); if((client_sockfd = socket(PF_INET,SOCK_STREAM,0))<0) { perror("socket"); return 1; } if(connect(client_sockfd,(struct sockaddr *)&server_addr,sizeof(struct sockaddr))<0) { perror("connect"); return 1; } len = recv(client_sockfd,buf,BUFSIZE,0); buf[len] = '/0'; printf("%s/n",buf); while(1) { printf("Entet string to send:"); scanf("%s",buf); if(!strcmp(buf,"quit")) break; len = send(client_sockfd,buf,strlen(buf),0); len = recv(client_sockfd,buf,BUFSIZE,0); buf[len]='/0'; printf("Receive:%s/n",buf); } close(client_sockfd); return 0; } UDP编程服务器端一般步骤: 1、创建一个socket,用函数socket(); 2、设置socket属性,用函数setsockopt();* 可选 3、绑定IP地址、端口等信息到socket上,用函数bind();* 可选 4、设置要连接的对方的IP地址和端口等属性; 5、连接服务器,用函数connect(); 6、收发数据,用函数send()和recv(),或者read()和write(); 7、关闭网络连接; UDP编程客户端一般步骤: 1、创建一个socket,用函数socket(); 2、设置socket属性,用函数setsockopt();* 可选 3、绑定IP地址、端口等信息到socket上,用函数bind();* 可选 4、设置对方的IP地址和端口等属性; 5、发送数据,用函数sendto(); 6、关闭网络连接; [c-sharp] view plaincopyprint? 01.UDP服务器端udpserver.c 02.#include 03.#include 04.#include 05.#include 06.#include 07.#include 08. 09.#define BUFSIZE 1024 10. 11.int main(int argc,char **argv) 12.{ 13. int server_sockfd; 14. int len; 15. struct sockaddr_in server_addr,client_addr; 16. int sin_size; 17. char buf[BUFSIZE]; 18. 19. memset(&server_addr,0,sizeof(server_addr)); 20. server_addr.sin_family = AF_INET; 21. server_addr.sin_port = htons(8000); 22. server_addr.sin_addr.s_addr = INADDR_ANY; 23. 24. if((server_sockfd = socket(PF_INET,SOCK_DGRAM,0))<0) 25. { 26. perror("socket"); 27. return 1; 28. } 29. 30. if(bind(server_sockfd,(struct sockaddr *)&server_addr,sizeof(struct sockaddr))<0) 31. { 32. perror("bind"); 33. return 1; 34. } 35. 36. sin_size = sizeof(struct sockaddr_in); 37. 38. if((len = recvfrom(server_sockfd,buf,BUFSIZE,0,(struct sockaddr *)&client_addr,&sin_size))<0) 39. { 40. perror("recvfrom"); 41. return 1; 42. } 43. printf("received from %s/n",inet_ntoa(client_addr.sin_addr)); 44. buf[len] = '/0'; 45. printf("%s/n",buf); 46. close(server_sockfd); 47. return 0; 48.} UDP服务器端udpserver.c #include #include #include #include #include #include #define BUFSIZE 1024 int main(int argc,char **argv) { int server_sockfd; int len; struct sockaddr_in server_addr,client_addr; int sin_size; char buf[BUFSIZE]; memset(&server_addr,0,sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(8000); server_addr.sin_addr.s_addr = INADDR_ANY; if((server_sockfd = socket(PF_INET,SOCK_DGRAM,0))<0) { perror("socket"); return 1; } if(bind(server_sockfd,(struct sockaddr *)&server_addr,sizeof(struct sockaddr))<0) { perror("bind"); return 1; } sin_size = sizeof(struct sockaddr_in); if((len = recvfrom(server_sockfd,buf,BUFSIZE,0,(struct sockaddr *)&client_addr,&sin_size))<0) { perror("recvfrom"); return 1; } printf("received from %s/n",inet_ntoa(client_addr.sin_addr)); buf[len] = '/0'; printf("%s/n",buf); close(server_sockfd); return 0; } [c-sharp] view plaincopyprint? 01.UDP客服端udpclient.c 02.#include 03.#include 04.#include 05.#include 06.#include 07.#include 08. 09.#define BUFSIZE 1024 10. 11.int main(int argc,char **argv) 12.{ 13. int client_sockfd; 14. int len; 15. struct sockaddr_in server_addr; 16. int sin_size; 17. char buf[BUFSIZE]; 18. 19. memset(&server_addr,0,sizeof(server_addr)); 20. server_addr.sin_family = AF_INET; 21. server_addr.sin_port = htons(8000); 22. server_addr.sin_addr.s_addr = inet_addr("192.168.0.8"); 23. 24. if((client_sockfd = socket(PF_INET,SOCK_DGRAM,0))<0) 25. { 26. perror("socket"); 27. return 1; 28. } 29. 30. printf("Enter the send message/n"); 31. scanf("%s",buf); 32. sin_size = sizeof(struct sockaddr_in); 33. 34. if((len = sendto(client_sockfd,buf,strlen(buf),0,(struct sockaddr *)&server_addr,sin_size))<0) 35. { 36. perror("sendto"); 37. return 1; 38. } 39. close(client_sockfd); 40. return 0; 41.}
阅读(1871) | 评论(0) | 转发(0) |
0

上一篇:linux系统中ls命令用法详解

下一篇:没有了

给主人留下些什么吧!~~