1. 程式人生 > >Cfree windows c語言 socket

Cfree windows c語言 socket

size 參考 serve amp ces 數據 valid brush n)

server.c

#include <stdio.h>
#include <winsock2.h>
#include <winsock2.h>

#define SERVER_PORT 5208 //偵聽端口

int main()
{
    WORD wVersionRequested;
    WSADATA wsaData;
    int ret, nLeft, length;
    SOCKET sListen, sServer; //偵聽套接字,連接套接字
    struct sockaddr_in saServer, saClient; //地址信息   
    char *ptr;//用於遍歷信息的指針   
    //WinSock初始化
    wVersionRequested=MAKEWORD(2, 2); //希望使用的WinSock DLL 的版本
    ret=WSAStartup(wVersionRequested, &wsaData);
    if(ret!=0)
    {
        printf("WSAStartup() failed!\n");
        return;
    }
    //創建Socket,使用TCP協議
    sListen=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sListen == INVALID_SOCKET)
    {
        WSACleanup();
        printf("socket() faild!\n");
        return;
    }
    //構建本地地址信息
    saServer.sin_family = AF_INET; //地址家族
    saServer.sin_port = htons(SERVER_PORT); //註意轉化為網絡字節序
    saServer.sin_addr.S_un.S_addr = htonl(INADDR_ANY); //使用INADDR_ANY 指示任意地址
  
    //綁定
    ret = bind(sListen, (struct sockaddr *)&saServer, sizeof(saServer));
    if (ret == SOCKET_ERROR)
    {
        printf("bind() faild! code:%d\n", WSAGetLastError());
        closesocket(sListen); //關閉套接字
        WSACleanup();
        return;
    }
  
    //偵聽連接請求
    ret = listen(sListen, 5);
    if (ret == SOCKET_ERROR)
    {
        printf("listen() faild! code:%d\n", WSAGetLastError());
        closesocket(sListen); //關閉套接字
        return;
    }
  
    printf("Waiting for client connecting!\n");
    printf("Tips: Ctrl+c to quit!\n");
    //阻塞等待接受客戶端連接
 while(1)//循環監聽客戶端,永遠不停止,所以,在本項目中,我們沒有心跳包。
 {
  length = sizeof(saClient);
  sServer = accept(sListen, (struct sockaddr *)&saClient, &length);
  if (sServer == INVALID_SOCKET)
  {
   printf("accept() faild! code:%d\n", WSAGetLastError());
   closesocket(sListen); //關閉套接字
   WSACleanup();
   return;
  }       
  char receiveMessage[5000];
   nLeft = sizeof(receiveMessage);
  ptr = (char *)&receiveMessage;
  while(nLeft>0)
  {
   //接收數據
   ret = recv(sServer, ptr, 5000, 0);
   if (ret == SOCKET_ERROR)
   {
    printf("recv() failed!\n");
    return;
   }
   if (ret == 0) //客戶端已經關閉連接
   {
    printf("Client has closed the connection\n");
    break;
   }
   nLeft -= ret;
   ptr += ret;
  }  
    printf("receive message:%s\n", receiveMessage);//打印我們接收到的消息。
 
 } 
  //  closesocket(sListen);
  //  closesocket(sServer);
  //  WSACleanup();
  return 0;
}

client.c

#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#define SERVER_PORT 5208 //偵聽端口
int main()
{
   WORD wVersionRequested;
   WSADATA wsaData;
   int ret;
   SOCKET sClient; //連接套接字
   struct sockaddr_in saServer; //地址信息
   char *ptr;
   BOOL fSuccess = TRUE;
   //WinSock初始化
   wVersionRequested = MAKEWORD(2, 2); //希望使用的WinSock DLL的版本
   ret = WSAStartup(wVersionRequested, &wsaData);
   if(ret!=0)
   {
        printf("WSAStartup() failed!\n");
        return;
   }
   //確認WinSock DLL支持版本2.2
   if(LOBYTE(wsaData.wVersion)!=2 || HIBYTE(wsaData.wVersion)!=2)
   {
        WSACleanup();
        printf("Invalid WinSock version!\n");
        return;
   }
   //創建Socket,使用TCP協議
   sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
   if (sClient == INVALID_SOCKET)
   {
        WSACleanup();
        printf("socket() failed!\n");
        return;
   }
   //構建服務器地址信息
   saServer.sin_family = AF_INET; //地址家族
   saServer.sin_port = htons(SERVER_PORT); //註意轉化為網絡節序
   saServer.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
   //連接服務器
   ret = connect(sClient, (struct sockaddr *)&saServer, sizeof(saServer));
   if (ret == SOCKET_ERROR)
   {
        printf("connect() failed!\n");
        closesocket(sClient); //關閉套接字
        WSACleanup();
        return;
   }
 
  
   char sendMessage[]="hello this is client message!"; 
   ret = send (sClient, (char *)&sendMessage, sizeof(sendMessage), 0);
   if (ret == SOCKET_ERROR)
   {
        printf("send() failed!\n");
   }
   else
        printf("client info has been sent!");
   closesocket(sClient); //關閉套接字
   WSACleanup();
   return 0;
}

Cfree的配置

構建>構建選項>連接>參數

-lws2_32

技術分享

參考

windows下用c實現Socket通信_Hope_新浪博客
http://blog.sina.com.cn/s/blog_49f485700100mkjk.html

Cfree windows c語言 socket