Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4377
  • 博文数量: 2
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2014-10-21 12:54
文章分类

全部博文(2)

文章存档

2014年(2)

我的朋友
最近访客

分类: 嵌入式

2014-10-21 12:56:01

转:http://blog.csdn.net/rangf/article/details/8315728

linux端:
  1. #include   
  2. #include   
  3. #include   
  4. #include   
  5. #include   
  6. #include   
  7. #include   
  8. #include   
  9. #define MYPORT 3333  
  10. #define BACKLOG 10  
  11. main()  
  12. {  
  13.  int sockfd, new_fd;  
  14.  struct sockaddr_in my_addr;  
  15.  struct sockaddr_in their_addr;  
  16.  int sin_size;  
  17.  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {  
  18.     perror("socket");  
  19.     exit(1);  
  20.  }  
  21.  my_addr.sin_family = AF_INET;  
  22.  my_addr.sin_port = htons(MYPORT);  
  23.  my_addr.sin_addr.s_addr = htonl(INADDR_ANY);  
  24.  bzero(&(my_addr.sin_zero),0);  
  25.  if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))== -1) {  
  26.     perror("bind");  
  27.     exit(1);  
  28.  }  
  29.  if (listen(sockfd, BACKLOG) == -1) {  
  30.     perror("listen");  
  31.     exit(1);  
  32.  }  
  33.  while(1) {  
  34.  sin_size = sizeof(struct sockaddr_in);  
  35.  if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr,&sin_size)) == -1) {  
  36.     perror("accept");  
  37.     continue;  
  38.  }  
  39.  printf("server: got connection from %s\n",inet_ntoa(their_addr.sin_addr));  
  40.  if (!fork()) {  
  41.  if (send(new_fd, "Hello, world!\n", 14, 0) == -1)  
  42.  perror("send");  
  43.  close(new_fd);  
  44.  exit(0);  
  45.  }  
  46.  close(new_fd);  
  47.  while(waitpid(-1,NULL,WNOHANG) > 0);  
  48.  }  
  49. }  

 window端:
  1. // client.cpp : Defines the entry point for the console application.  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include  
  6. //#include  
  7. //#include    
  8. #include  
  9. #pragma comment (lib,"WS2_32.lib")  
  10.   
  11. #include  
  12. //using namespace std;  
  13.   
  14. int _tmain(int argc, _TCHAR* argv[])  
  15. {  
  16.      int i;  
  17.  char recvBuffer[255];  
  18.  WORD wVersionRequested; //typedef unsigned short WORD; 2字节  
  19.  WSADATA wsaData;  
  20.  //WSADATA 包含了Windows Socket执行的信息。  
  21.  int err;  
  22.  wsaData.wVersion =MAKEWORD(1,1);  
  23.  //这个宏创建一个被指定变量连接而成的WORD变量。返回一个WORD变量。  
  24.  //第一个是socket库版本,第二个是取得的版本号。  
  25.  err=WSAStartup(wVersionRequested,&wsaData); //return 0 if successful  
  26.  if(err!=0){  
  27.   printf("Call WSAStart ERROR!");  
  28.   exit(1);  
  29.  }   //终止对WinSock库的使用  
  30.  //if(LOBYTE(wsaData.wVersion)!=1|| HIBYTE(wsaData.wHighVersion)!=1)  
  31.  //{   
  32.  //  WSACleanup();  
  33.  //  exit(0);  
  34.  //}  
  35.  //typedef unsigined int   SOCKET;//创建用与监听的套接字  
  36.  SOCKET SocketClient=socket(AF_INET,SOCK_STREAM,0);       //0表示让系统自己选择协议  
  37.  //定义地址结构体//填入服务器端的ip地址和端口号   
  38.  SOCKADDR_IN addrSrv;   
  39.  //转换为TCP/IP network byte order //32bit    
  40.  addrSrv.sin_addr.S_un.S_addr=inet_addr("172.17.51.81"); //ip 172.17.51.81  
  41.  addrSrv.sin_family=AF_INET;                              //family address  
  42.  addrSrv.sin_port=htons(3333);                            //16bit端口号  
  43.  printf("Connect to server...\n");  
  44.  i=connect(SocketClient,(sockaddr *)&addrSrv,sizeof(SOCKADDR_IN)); //指向要建立连接的数据结构    
  45.  if(i<0){  
  46.   printf("%i\n",WSAGetLastError());  
  47.   printf("连接到172.17.51.81:3333错误!");  
  48.   exit(1);  
  49.  }  
  50.  recv(SocketClient,recvBuffer,255,0);  
  51.  printf("%s\n",recvBuffer);   //send(socketClient,recvBuffer,20,0);  
  52.  closesocket(SocketClient);  
  53.  WSACleanup();  
  54.     return 0;  
  55. }  

 

     注:

    1、windows下控制台程序,需要注意加链接库ws2_32.lib

    2、文件名以cpp结尾,解决SOCKET等符号找不到问题

    3、#include 解决exit符号找不到问题

    4、补充第1点:#pragma comment (lib,"WS2_32.lib")

 

阅读(671) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇: socket在windows下和linux下的区别

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