Windows Socket 编程:TCP客户端

时间:2022-01-31 05:13:02

#include <stdio.h>

#include <stdlib.h>

#include <winsock2.h>


int main(int argc,char * argv[])

{

char message[30];

WSADATA wsaData;

SOCKET hSocket;

SOCKADDR_IN servAddr;


WSAStartup(MAKEWORD(2,2),&wsaData);

hSocket = socket(PF_INET,SOCK_STREAM,0);


memset(&servAddr,0,sizeof(servAddr));

servAddr.sin_family = AF_INET;

servAddr.sin_addr.s_addr = inet_addr(argv[1]);

servAddr.sin_port = htons(atoi(argv[2]));


connect(hSocket,(SOCKADDR *)&servAddr,sizeof(servAddr));


recv(hSocket,message,sizeof(message) - 1,0);

printf("%s",message);


closesocket(hSocket);

WSACleanup();


return 0;

}


Windows Socket 编程:TCP客户端