高手们,请进来指点一下!

我正在设计一个关于篮球比赛的E---R图,请大家指点一下怎么设计?

阅读更多

socket高手请进来指点一下

03-28

写了一个用socket收发数据(send,recv)的console程序,客户端发送,服务器端收数据并回显,最大线程数是十个(每个客户端一个),现把服务器端代码贴出来,望高手指点一下:rn#include "stdafx.h"rnrn#define NUMBER 10rnrnSOCKET NewSocket[NUMBER];rnrn//线程处理段,解决多连接的数据处理问题rnvoid ProceData(PVOID ii)rnrn char DataBuffer[1024];rn memset(DataBuffer,0,sizeof(DataBuffer));rn int i = *((int *)ii);rn int Ret;rn while(1)rn rn if ((Ret = recv(NewSocket[i],DataBuffer,sizeof(DataBuffer),0))!= SOCKET_ERROR)rn rn if (Ret > 0)rn rn //printf("We Have received %d bytes from the client\n",Ret);rn printf("收到的数据是: %s\n",DataBuffer);rn rn rn Sleep(1);rn rnrnint _tmain(int argc, _TCHAR* argv[])rnrn WSADATA wsaData;rn SOCKET LisenSocket;rn SOCKADDR_IN Serveraddr,Clientaddr;rn int Ret,ClientaddrLen;rn char DataBuffer[1024];rn int port = 5500;rn BOOL sc = TRUE;rn HANDLE hThd[NUMBER];rn int nThd;rnrn //初始化winsock 版本库rn if ((Ret = WSAStartup(MAKEWORD(2,2),&wsaData)) != 0)rn rn printf("WSAStartup failed with error code %d \n", Ret);rn return -1;rn rnrn //创建socket套结字rnrn if ((LisenSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)) == INVALID_SOCKET)rn rn printf("Create the socket failed with error code %d\n",WSAGetLastError());rn WSACleanup();rn return -1;rn rn Serveraddr.sin_family = AF_INET;rn Serveraddr.sin_port = htons(port); //将端口地址转换为网络字节rn Serveraddr.sin_addr.s_addr = htonl(INADDR_ANY); //将ip地址转换为网络字节rnrn if ((Ret = bind(LisenSocket,(SOCKADDR *)&Serveraddr,sizeof(Serveraddr))) == SOCKET_ERROR)rn rn printf("bind the socket failed with error code %d\n",WSAGetLastError());rn closesocket(LisenSocket);rn WSACleanup();rn return -1;rn rnrn //监听客户机连接,确定最大连接数rnrn listen(LisenSocket,NUMBER);rnrn //接受一个新的连接rn rn printf("We are waiting a connection on port %d.\n", port);rn rn int i = 0;rn int j;rn ClientaddrLen = sizeof(Clientaddr);rn while(i < NUMBER)rn rn if ((NewSocket[i] = accept(LisenSocket, (SOCKADDR *)&Clientaddr,&ClientaddrLen)) == INVALID_SOCKET)rn rn Ret = WSAGetLastError();rn printf("We have tryed to accept a socket for the %d times...\n", ++i);rn switch(Ret)rn rn case WSANOTINITIALISED:rn printf("A successful WSAStartup call must occur before using this function.\n");rn break;rn case WSAENETDOWN:rn printf("The network subsystem has failed.\n");rn break;rn case WSAEFAULT:rn printf("The addrlen parameter is too small or addr is not a valid part of the user address space\n");rn break;rn case WSAEINTR:rn printf("A blocking Windows Sockets 1.1 call was canceled through WSACancelBlockingCall.\n");rn break;rn case WSAEINPROGRESS:rn printf("A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function.\n");rn break;rn case WSAEINVAL:rn printf("The listen function was not invoked prior to accept.\n");rn break;rn case WSAEMFILE:rn printf("The queue is nonempty upon entry to accept and there are no descriptors available\n");rn break;rn case WSAENOBUFS:rn printf("No buffer space is available.\n");rn break;rn case WSAENOTSOCK:rn printf("The descriptor is not a socket\n");rn break;rn case WSAEOPNOTSUPP:rn printf("The referenced socket is not a type that supports connection-oriented service.\n");rn break;rn case WSAEWOULDBLOCK:rn printf("The socket is marked as nonblocking and no connections are present to be accepted\n");rn break;rn default:rn printf("a unknown error eccured\n");rn break;rn rn //Sleep(100);rn rn elsern rn //inet_ntoa()函数将ipv4 internet地址转换为char far *rn j = i;rn printf("We successfully got a connection from %s:%d.\n",inet_ntoa(Clientaddr.sin_addr), ntohs(Clientaddr.sin_port));rn hThd[i] = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)&ProceData,(LPVOID)&j,0,(PULONG)&nThd);rn i++;rn rn rnrn rn Ret = GetTickCount();rn Sleep(90000000000000);rn //释放空间rn for (int k = 0; k < i; k++)rn rn if (hThd[k] != NULL)rn CloseHandle(hThd[k]);rn if (NewSocket[k] != INVALID_SOCKET)rn closesocket(NewSocket[k]);rn rn Ret = GetTickCount() - Ret;rn printf("The thread has been in %d seconds\n",Ret);rn printf("over\n");rn rn rn //关闭winsockrn WSACleanup();rn return 0;rnrnrnrn先提出问题如下:rn一:本来想在createthread()时,将第三个参数直接赋值为NewSocket[i],但是总提示参数不匹配,只好将其下标传过去,并将NewSocket[i]定义成了全局变量,不知道该如何解决??rn二:在发送端如果发送的字符中包含空格,则空格后面的字符都收不到??rn三:如果启十个客户端并频繁的发送数据,会出现丢包,即收到的数据为空??rn四:主线程不知道该如何控制子线程,感知它的存在与否?rn五:若客户端close一个,服务器怎么知道,并释放相应的空间?rn六:第一次写这方面的东西,处理方法上一定有很多不合理的地方,望指点一下。rnrnrn 呵呵,提的问题太多了,若哪位高手有完美的代码,不吝赐教吧。多谢了!!rnrn

没有更多推荐了,返回首页