该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
//server
#include
#include
#include
#include
#include
#define PORT 100
#define socklen_tint
#pragma comment(lib, "wsock32.lib")
SOCKET s;
SOCKET client;
sockaddr_in from;
void ServerThread()
{
char message[256];
while(1)
{
strcpy(message,"");
int len=recv(client,message,sizeof(message),0);
if (strcmp(message,"")!=0)
{
cout<
//cout<
message[len]='\0';
cout<
}
}
}
int main()
{
sockaddr_in srv;
int fromlen=sizeof(from);
char message[256]="Welcome!\n";
WSADATA wsadata;
WORD VersionRequested=MAKEWORD(1,1);
if (WSAStartup(VersionRequested,&wsadata))
{
cout<
return 0;
}
s=socket(PF_INET,SOCK_STREAM,0);
if (s==INVALID_SOCKET)
{
cout<
return -1;
}
srv.sin_family=PF_INET;
srv.sin_addr.s_addr=htonl(INADDR_ANY);
srv.sin_port=htons(PORT);
if (bind(s,(struct sockaddr *)&srv,sizeof(srv))!=0)
{
cout<
closesocket(s);
return -1;
}
cout<
if (listen(s,5)!=0)
{
cout<
return -1;
}
while(1)
{
client=accept(s,(struct sockaddr *)&from,&fromlen);
send(client,message,strlen(message),0);
cout<
DWORD Sthread;
CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(ServerThread),NULL,0,&Sthread);
while(fgets(message,strlen(message),stdin))
{
cout<
send(client,message,strlen(message),0);
}
}
closesocket(s);
WSACleanup();
}