client.cpp
//WSAStartup socket bind listen accept send/recv closesocket WSACleanup
#include <winsock2.h>
#pragma comment (lib,"ws2_32")
int main(int argc , char* grgv[])
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2),&wsaData);
SOCKET s = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
sockaddr_in addr;
addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
addr.sin_port = htons(827);
addr.sin_family = PF_INET;
bind(s,(SOCKADDR*)&addr,sizeof(SOCKADDR));
listen(s,1);
SOCKADDR clientAddr;
int nSize = sizeof(SOCKADDR);
SOCKET clientSock;
clientSock = accept(s,(SOCKADDR*)&clientAddr,&nSize);
send(clientSock,"hello client \r\n",strlen("hello client \r\n" + sizeof(char)),NULL);
closesocket(clientSock);
closesocket(s);
WSACleanup();
return 0;
}
#include <stdio.h>
#include <winsock2.h>
#pragma comment (lib,"ws2_32")
//流程
//WSAStarup socket connect recv/send closesocket WSACleanup
int main(int argc,char* argv[])
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2),&wsaData);
SOCKET s = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
sockaddr_in addr;
addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
addr.sin_family = PF_INET;
addr.sin_port = htons(827);
connect(s,(SOCKADDR*)&addr,sizeof(SOCKADDR));
char szBuffer[MAXBYTE] = {0};
recv(s,szBuffer,MAXBYTE,NULL);
printf("szBuffer = %s \r\n",szBuffer);
closesocket(s);
WSACleanup();
return 0;
}
#include <winsock2.h>
#pragma comment (lib,"ws2_32")
//流程
//WSAStarup socket connect recv/send closesocket WSACleanup
int main(int argc,char* argv[])
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2),&wsaData);
SOCKET s = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
sockaddr_in addr;
addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
addr.sin_family = PF_INET;
addr.sin_port = htons(827);
connect(s,(SOCKADDR*)&addr,sizeof(SOCKADDR));
char szBuffer[MAXBYTE] = {0};
recv(s,szBuffer,MAXBYTE,NULL);
printf("szBuffer = %s \r\n",szBuffer);
closesocket(s);
WSACleanup();
return 0;
}
Server.cpp
//WSAStartup socket bind listen accept send/recv closesocket WSACleanup
#include <winsock2.h>
#pragma comment (lib,"ws2_32")
int main(int argc , char* grgv[])
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2),&wsaData);
SOCKET s = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
sockaddr_in addr;
addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
addr.sin_port = htons(827);
addr.sin_family = PF_INET;
bind(s,(SOCKADDR*)&addr,sizeof(SOCKADDR));
listen(s,1);
SOCKADDR clientAddr;
int nSize = sizeof(SOCKADDR);
SOCKET clientSock;
clientSock = accept(s,(SOCKADDR*)&clientAddr,&nSize);
send(clientSock,"hello client \r\n",strlen("hello client \r\n" + sizeof(char)),NULL);
closesocket(clientSock);
closesocket(s);
WSACleanup();
return 0;
}