原文地址:http://blog.neohope.com/2012/01/14/mingw_socket_basic/
1.客户端简单例子
myc.c
  1. #define MINGW32
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. #ifdef MINGW32
  8. #include <winsock2.h>
  9. #else
  10. #include <sys/socket.h>
  11. #include <arpa/inet.h>
  12. #endif
  13.  
  14. #define MAXLINE 1024
  15.  
  16. int main(int argc,char **argv)
  17. {
  18.   int sockfd,n;
  19.   char receline[MAXLINE+1];
  20.   struct sockaddr_in serveraddr;
  21.  
  22.   //输入参数太少,退出
  23.   if(argc != 2)
  24.   {
  25.     printf("Usage :%s IP_address\n", argv[0]);
  26.     exit(0);
  27.   }
  28.  
  29.   #ifdef MINGW32
  30.   //Winsows下启用socket
  31.   WSADATA wsadata;
  32.   if(WSAStartup(MAKEWORD(1,1),&wsadata)==SOCKET_ERROR)
  33.   {
  34.     printf("WSAStartup() fail\n");
  35.     exit(0);
  36.   }
  37.   #endif
  38.  
  39.   //建立socket
  40.   if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
  41.   {
  42.     printf("socket() fail\n");
  43.     exit(0);
  44.   }
  45.  
  46.   //设置协议及Port
  47.   memset(&serveraddr,0,sizeof(serveraddr));
  48.   serveraddr.sin_family = AF_INET;
  49.   serveraddr.sin_port=htons(1024);
  50.  
  51.   //设置IP
  52.   serveraddr.sin_addr.s_addr=inet_addr(argv[1]);
  53.  
  54.   //连接
  55.   if(connect(sockfd,(struct sockaddr *)&serveraddr,sizeof(serveraddr))==-1)
  56.   {
  57.     printf("connect() fail\n");
  58.     exit(0);
  59.   }
  60.  
  61.   //读取数据并输入到标准输出
  62.   #ifdef MINGW32
  63.   while((n=recv(sockfd,receline,MAXLINE,0))>0)
  64.   #else
  65.   while((n=read(sockfd,receline,MAXLINE))>0)
  66.   #endif
  67.   {
  68.     receline[n]=0;
  69.     if(fputs(receline,stdout)==EOF)
  70.     {
  71.       printf("fputs() error\r\n");
  72.     }
  73.   }
  74.  
  75.   //没有获取数据
  76.   if(n<0)
  77.   {
  78.     printf("read() fail\n");
  79.   }
  80.  
  81.   #ifdef MINGW32
  82.   //Winsows下关闭socket
  83.   closesocket(sockfd);
  84.   WSACleanup();
  85.   #endif
  86.  
  87.   exit(0);
  88. }
2.服务端简单例子
mys.c
  1. #define MINGW32
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. #ifdef MINGW32
  8. #include <winsock2.h>
  9. #else
  10. #include <sys/socket.h>
  11. #include <arpa/inet.h>
  12. #endif
  13.  
  14. #define LISTENQ 10
  15.  
  16. int main(int argc,char *argv[])
  17. {
  18.   int serverfd,connectfd;
  19.   struct sockaddr_in serveraddr;
  20.   char buff[1024];
  21.   time_t tlick;
  22.   int iRet;
  23.  
  24.   #ifdef MINGW32
  25.   //Winsows下启用socket
  26.   WSADATA wsadata;
  27.   if(WSAStartup(MAKEWORD(1,1),&wsadata)==SOCKET_ERROR)
  28.   {
  29.     printf("WSAStartup() fail\n");
  30.     exit(0);
  31.   }
  32.   #endif
  33.  
  34.   //新建socket
  35.   printf("socket()\n");
  36.   serverfd=socket(AF_INET,SOCK_STREAM,0);
  37.   if(serverfd==-1)
  38.   {
  39.     printf("socket() fail\n");
  40.     exit(0);
  41.   }
  42.  
  43.   //清零
  44.   memset(&serveraddr,0,sizeof(serveraddr));
  45.  
  46.   //设置协议
  47.   serveraddr.sin_family=AF_INET;
  48.   //设置IP
  49.   serveraddr.sin_addr.s_addr=htonl(INADDR_ANY);
  50.   //设置Port
  51.   serveraddr.sin_port=htons(1024);
  52.  
  53.   //绑定端口,监听1024端口的任何请求
  54.   printf("bind()\n");
  55.   iRet=bind(serverfd,(struct sockaddr*)&serveraddr,sizeof(serveraddr));
  56.   if(iRet==-1)
  57.   {
  58.     printf("bind() fail\n");
  59.     exit(0);
  60.   }
  61.  
  62.   //监听端口,最大并发数10
  63.   printf("listen()\n");
  64.   iRet=listen(serverfd,LISTENQ);
  65.   if(iRet==-1)
  66.   {
  67.     printf("listen() fail\n");
  68.     exit(0);
  69.   }
  70.  
  71.   //接受请求,发送主机时间
  72.   for(;;)
  73.   {
  74.     printf("Waiting for connection...\n");
  75.     //接受请求
  76.     connectfd=accept(serverfd,(struct sockaddr*)NULL,NULL);
  77.     //获取时间
  78.     tlick=time(NULL);
  79.     //格式化时间
  80.     snprintf(buff,sizeof(buff),"From mys:\n%s",ctime(&tlick));
  81.     //写入时间
  82.     //关闭请求
  83.     #ifdef MINGW32
  84.     send(connectfd,buff,strlen(buff),0);
  85.     closesocket(connectfd);
  86.     #else
  87.     write(connectfd,buff,strlen(buff));
  88.     close(connectfd);
  89.     #endif
  90.   }
  91.  
  92.   #ifdef MINGW32
  93.   //Winsows下关闭socket
  94.   closesocket(serverfd);
  95.   WSACleanup();
  96.   #endif
  97.  
  98.   //退出
  99.   exit(0);
  100. }
3.Makefile
  1. all:mys myc
  2. mys:mys.c
  3. gcc -g -o mys mys.c -l wsock32
  4. myc:myc.c
  5. gcc -g -o myc myc.c -l wsock32
  6. clean:
  7. del *~ *.o *.exe




11-21 13:54