MinGW中Socket基础

1.客户端简单例子
myc.c

#define MINGW32

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef MINGW32
#include <winsock2.h>
#else
#include <sys/socket.h>
#include <arpa/inet.h>
#endif

#define MAXLINE 1024

int main(int argc,char **argv) 
{
  int  sockfd,n;
  char  receline[MAXLINE+1];
  struct  sockaddr_in serveraddr;

  //输入参数太少,退出
  if(argc != 2) 
  { 
    printf("Usage :%s  IP_address\n", argv[0]);
    exit(0);
  }

  #ifdef MINGW32
  //Winsows下启用socket
  WSADATA wsadata;
  if(WSAStartup(MAKEWORD(1,1),&wsadata)==SOCKET_ERROR)
  {
    printf("WSAStartup() fail\n");
    exit(0);
  }
  #endif
  
  //建立socket
  if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
  {
    printf("socket() fail\n");
    exit(0);
  }

  //设置协议及Port
  memset(&serveraddr,0,sizeof(serveraddr));
  serveraddr.sin_family = AF_INET;
  serveraddr.sin_port=htons(1024);

  //设置IP
  serveraddr.sin_addr.s_addr=inet_addr(argv[1]);

  //连接
  if(connect(sockfd,(struct sockaddr *)&serveraddr,sizeof(serveraddr))==-1)
  {
    printf("connect() fail\n");
    exit(0);
  }

  //读取数据并输入到标准输出
  #ifdef MINGW32
  while((n=recv(sockfd,receline,MAXLINE,0))>0)
  #else
  while((n=read(sockfd,receline,MAXLINE))>0)
  #endif
  { 
    receline[n]=0;
    if(fputs(receline,stdout)==EOF)
    {
      printf("fputs() error\r\n");
    }
  }

  //没有获取数据
  if(n<0) 
  {
    printf("read() fail\n");
  }

  #ifdef MINGW32
  //Winsows下关闭socket
  closesocket(sockfd);
  WSACleanup();
  #endif

  exit(0);
}
&#91;/code&#93;

2.服务端简单例子
mys.c
&#91;code lang="c"&#93;
#define MINGW32

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef MINGW32
#include <winsock2.h>
#else
#include <sys/socket.h>
#include <arpa/inet.h>
#endif

#define LISTENQ 10

int main(int argc,char *argv[])
{
  int  serverfd,connectfd;
  struct  sockaddr_in serveraddr;
  char  buff[1024];
  time_t  tlick;
  int  iRet;

  #ifdef MINGW32
  //Winsows下启用socket
  WSADATA wsadata;
  if(WSAStartup(MAKEWORD(1,1),&wsadata)==SOCKET_ERROR)
  {
    printf("WSAStartup() fail\n");
    exit(0);
  }
  #endif

  //新建socket
  printf("socket()\n");
  serverfd=socket(AF_INET,SOCK_STREAM,0);
  if(serverfd==-1)
  {
    printf("socket() fail\n");
    exit(0);
  }

  //清零
  memset(&serveraddr,0,sizeof(serveraddr));

  //设置协议
  serveraddr.sin_family=AF_INET;
  //设置IP
  serveraddr.sin_addr.s_addr=htonl(INADDR_ANY);
  //设置Port
  serveraddr.sin_port=htons(1024);

  //绑定端口,监听1024端口的任何请求
  printf("bind()\n");
  iRet=bind(serverfd,(struct sockaddr*)&serveraddr,sizeof(serveraddr));
  if(iRet==-1)
  {
    printf("bind() fail\n");
    exit(0);
  }

  //监听端口,最大并发数10
  printf("listen()\n");
  iRet=listen(serverfd,LISTENQ);
  if(iRet==-1)
  {
    printf("listen() fail\n");
    exit(0);
  }

  //接受请求,发送主机时间
  for(;;)
  {
    printf("Waiting for connection...\n");
    //接受请求
    connectfd=accept(serverfd,(struct sockaddr*)NULL,NULL);
    //获取时间    
    tlick=time(NULL);
    //格式化时间    
    snprintf(buff,sizeof(buff),"From mys:\n%s",ctime(&tlick));
    //写入时间
    //关闭请求
    #ifdef MINGW32
    send(connectfd,buff,strlen(buff),0);
    closesocket(connectfd);
    #else
    write(connectfd,buff,strlen(buff));
    close(connectfd);
    #endif
  }

  #ifdef MINGW32
  //Winsows下关闭socket
  closesocket(serverfd);
  WSACleanup();
  #endif

  //退出
  exit(0);
}

3.Makefile

all:mys myc

mys:mys.c
	gcc -g -o mys mys.c -l wsock32

myc:myc.c
	gcc -g -o myc myc.c -l wsock32

clean:
	del *~ *.o *.exe

Comments are closed.