服务器端
/
#include "stdafx.h"
#include <winsock2.h>#include <stdio.h>
#include <windows.h>
#pragma comment(lib,"WS2_32.lib")
int main(int argc, char* argv[])
{
// printf("Hello World!\n");
WSADATA data;
WORD w =MAKEWORD(2,0);
char sztext[]="欢迎你\r\n";
::WSAStartup(w,&data);
SOCKET s,s1;
s=::socket(AF_INET,SOCK_STREAM,0);
sockaddr_in addr,addr2;
int n = sizeof (addr2);
addr.sin_family=AF_INET;
addr.sin_port=htons(75);
addr.sin_addr.S_un.S_addr=INADDR_ANY; //inet_addr("127.0.0.1")
::bind(s,(sockaddr*)&addr,sizeof(addr));
::listen(s,5);
printf("服务器已经启动\r\n");
while(true)
{
s1=::accept(s,(sockaddr*)&addr2,&n);
if(s1!=NULL)
{
printf("%s 已经连上\r\n",inet_ntoa(addr2.sin_addr));
::send(s1,sztext,sizeof(sztext),0);
}
::closesocket(s);
::closesocket(s1);
::WSACleanup();
if(getchar())
{
return 0 ;
}
else
{
::Sleep(100);
}
}
}
客户端
#include "stdafx.h"
#include <winsock2.h>
#include <stdio.h>
#include <windows.h>
#pragma comment(lib,"WS2_32.lib")
int main(int argc, char* argv[])
{
// printf("Hello World!\n");
WSADATA data;
WORD w=MAKEWORD(2,0);
::WSAStartup(w,&data);
SOCKET s;
char sztext[10]={0};
s=::socket(AF_INET,SOCK_STREAM,0);
sockaddr_in addr;
addr.sin_family=AF_INET;
addr.sin_port=htons(75);
addr.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
printf("客户端已经启动\r\n");
::connect(s,(sockaddr*)&addr,sizeof(addr));
::recv(s,sztext,sizeof(sztext),0);
printf("%s\r\n",sztext);
::closesocket(s);
::WSACleanup();
if(getchar())
{
return 0;
}
else
{
::Sleep(100);
}
}