用VC++建立一个TCP服务器,求例子看下
发布网友
发布时间:2022-04-25 20:15
我来回答
共2个回答
热心网友
时间:2022-06-17 02:22
//这个是我自己写的,用来测试,编译通过
// t.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdlib.h>
#include <WinSock2.h>
#include <string>
#include <sstream>
#include <memory>
#pragma comment (lib,"ws2_32.lib")
//HANDLE hevent;
DWORD WINAPI acceptpro(LPVOID lp);
DWORD WINAPI recvpro(LPVOID lp);
struct _cinf
{
SOCKET s;
HANDLE hevn;
SOCKADDR_IN saddr;
}cinf;
int flag=0;
CRITICAL_SECTION cc;
int _tmain(int argc, _TCHAR* argv[])
{
cinf.hevn=CreateEvent(NULL,false,false,NULL);
//cinf.hevn=WSACreateEvent();
InitializeCriticalSection(&cc);
WSADATA wsaData;
int err = WSAStartup( MAKEWORD(2,2),&wsaData);
if (err!=0)
{
WSACleanup();
}
SOCKET sListen;
// int a=4;
// int sendBufSize=1024*10;
//
// int fff=setsockopt(sListen,SOL_SOCKET,SO_RCVBUF,(char *)&sendBufSize,a);
// if (SOCKET_ERROR==fff)
// {
//
// cout<<"setsockopt error:"<<WSAGetLastError()<<endl;
//// continue;
// }
SOCKADDR_IN addr;
addr.sin_family=AF_INET;
addr.sin_port=htons(8888);
addr.sin_addr.S_un.S_addr=inet_addr("192.168.232.129");
sListen=socket(AF_INET,SOCK_STREAM,0);
bind(sListen,(sockaddr *)&addr,sizeof(SOCKADDR_IN));
int error=listen(sListen,SOMAXCONN);
if (SOCKET_ERROR ==error)
{
cout<<"listen fail"<<endl;
}
// hevent=CreateEvent(NULL,false,false,NULL);
CreateThread(NULL,0,acceptpro,(LPVOID)&sListen,0,NULL);
char qq;
EnterCriticalSection(&cc);
cout<<" enter q quit "<<endl;
LeaveCriticalSection(&cc);
while(1)
{
cin>>qq;
if (qq=='q')
{
cout<<"结束server"<<endl;
// SetEvent(hevent);
closesocket(sListen);
break;
}
}
system("pause");
return 0;
}
DWORD WINAPI acceptpro(LPVOID lp)
{
EnterCriticalSection(&cc);
cout<<" acceptting "<<endl;
LeaveCriticalSection(&cc);
while(flag==0)
{
SOCKET sListen=*(SOCKET*)lp;
SOCKADDR_IN remoteAddr;
int len=sizeof(SOCKADDR_IN);
SOCKET snew=accept(sListen,(sockaddr *)&remoteAddr,&len);
if (INVALID_SOCKET==snew)
{
cout<<"accept error "<<"退出 accept线程"<<endl;
return 0;
}
// cinf.hevn=recvEvent;
cinf.s=snew;
cinf.saddr=remoteAddr;
WSAEventSelect(cinf.s,cinf.hevn,FD_READ);
//CreateThread(NULL,0,recvpro,(LPVOID)&cinf,0,NULL);
EnterCriticalSection(&cc);
cout<<"新连接,ip:"<<inet_ntoa(*(in_addr*)&(remoteAddr.sin_addr.S_un.S_addr))<<"port:"<<ntohs(remoteAddr.sin_port)<<endl;
LeaveCriticalSection(&cc);
}
}
DWORD WINAPI recvpro(LPVOID lp)
{
EnterCriticalSection(&cc);
cout<<"recv"<<endl;
LeaveCriticalSection(&cc);
while(1)
{
_cinf * minf=(_cinf*)lp;
cout<<"__wait__"<<endl;
DWORD re=WaitForSingleObject(minf->hevn,INFINITE);
//ResetEvent(minf->hevn);
cout<<"__set__"<<endl;
if (0==re)
{
/* char buf[512];
recv(minf->s,buf,512,0);
*/ int a=4;
int recvBufSize=0;
int fff=getsockopt(minf->s,SOL_SOCKET,SO_RCVBUF,(char *)&recvBufSize,&a);
if (SOCKET_ERROR==fff)
{
cout<<"setsockopt error:"<<WSAGetLastError()<<endl;
continue;
}
EnterCriticalSection(&cc);
cout<<" 连接,ip:"<<inet_ntoa(*(in_addr*)&(minf->saddr.sin_addr.S_un.S_addr))<<" port:"<<ntohs(minf->saddr.sin_port)<<" recvBufSize: "<<recvBufSize<<endl;
EnterCriticalSection(&cc);
// WSAEventSelect(minf->s,cinf.hevn,FD_READ);
//WSASetEvent(minf.hevn);
}
else
{
cout<<"re="<<re<<endl;
// cout<<"连接,ip:"<<inet_ntoa(*(in_addr*)&(minf.saddr.sin_addr.S_un.S_addr))<<"port:"<<ntohs(minf.saddr.sin_port)<<"WaitForSingleObject:error "<<endl;
}
}
}
热心网友
时间:2022-06-17 02:23
wsastartup()->socket()->bind()->listen()->recv/send->clearwsa()->closesocket()