请教socket编程一个简单问题? (50分)

  • 主题发起人 主题发起人 toitorse
  • 开始时间 开始时间
T

toitorse

Unregistered / Unconfirmed
GUEST, unregistred user!
程序:....
//开始接收连接
int len=sizeof(struct sockaddr);
listen(socketid,1);

//进入一个死循环
do
{
msgsocket=accept(socketid,(struct sockaddr *)&client,(int*)&len);
if (msgsocket==INVALID_SOCKET)
{
perror("there is error in open new sockets");
break;
}
else
do

{
memset(buffer,0,sizeof(buffer));
if ( (rval=recv(msgsocket,buffer,1024,MSG_PEEK))==-1 )
{
perror("there is error in recving");
break;
}
if (rval==0)
printf("ending connection");
else

printf("-->%s/n",buffer);

}while(rval!=0);
......
请问为什么在客户端发了信息以后,程序进入死循环,循环客户发的那句信息呢?
 
客户端的数据长度=1024?
如果发变长数据的话,一般做法是先发一个长度,再发数据内容,接收端
根据长度接受数据,否则可能接受不正确。
例如你写的if ( (rval=recv(msgsocket,buffer,1024,MSG_PEEK))==-1 )
不够正确,因为有时recv会接到<1024时就返回(因为信号或其他原因),因此接受
段自己应写个循环,确保接受的要么是0,要么一定=1024。。。
 
试试:
else
if(rval>0)
printf("-->%s/n",buffer);
 
如果是自定义的通讯模式最好定义一个数据包头的格式,
包头应该是定长的,至少要包括下面的内容
包的类型(或操作类型)
包的长度该长度可以可以包括包头的长度也可以不包括
包的编号(用于接收端反馈数据包是否正确接收)
还要定义一个反馈信息,当收到数据后首先要判别信息是否有效
如果有效返回一个正常反馈,如果无效需要反馈一个错误信息,
如果你不打算这么作就最好使用定长信息包!
 
只要每次发送的数据不超过一定长度,他这样也是可以的,请参考这个例子:
Server:
----------------------------------------------------
#include <stdio.h>
#include <sys/types.h>
#include <winsock2.h>
#include <memory.h>
#define SERVER_PORT 5432
#define MAX_PENDING 5
#define MAX_LINE 256
void
main()
{
struct sockaddr_in sin;
struct sockaddr_in sout;
char buf[MAX_LINE];
int len;
int lens;
SOCKET s,new_s;
WORD wVersionRequested;
WSADATA wsaData;
int err;

wVersionRequested = MAKEWORD( 2, 0 );
err = WSAStartup( wVersionRequested, &amp;wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
exit(1);
}

/*build address data structure */
memset((char *)&amp;sin,0,sizeof(struct sockaddr));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons(SERVER_PORT);
/*setup passive open*/
if ((s=socket(AF_INET,SOCK_STREAM,0)) == INVALID_SOCKET ){
perror("simplex-talk:socket");
exit(1);
}
if ((bind(s, (struct sockaddr *)&amp;sin, sizeof(sin))) != 0){
perror("simplex-talk:bind");
exit(1);
}
if ((listen(s,MAX_PENDING))!=0){
perror("simplex-talk:listen");
exit(1);
}

/*wait for connection, then
receive and print text */
while(1){
memcpy((char *)&amp;sout,(char *)&amp;sin,sizeof(struct sockaddr_in));
lens=sizeof(sout);
printf("waiting.../n");
if ((new_s =accept(s,(struct sockaddr *)&amp;sout,&amp;lens)) == INVALID_SOCKET) {
perror("simplex-talk:accept");
exit(1);
}
printf("connection established!/n");
while((len=recv(new_s,buf,sizeof(buf),0))>0){
fputs(buf,stdout);
}
printf("socket closed!/n");
closesocket(new_s);
}
}
=============================================
Client:
---------------------------------------------
#include <stdio.h>
#include <sys/types.h>
#include <winsock2.h>
#include <memory.h>
#define SERVER_PORT 5432
#define MAX_LINE 256
void
main(int argc,char * argv[])
{
struct sockaddr_in sin;
struct hostent *hp;
char *host;
char buf[MAX_LINE];
int s;
int len;
WORD wVersionRequested;
WSADATA wsaData;
int err;

if (argc==2){
host = argv[1];
}
else
{
fprintf(stderr,"usage:Client host/n");
exit(1);
}

wVersionRequested = MAKEWORD( 2, 0 );
err = WSAStartup( wVersionRequested, &amp;wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
exit(1);
}


/*translate host name into peer's IP address */
hp = gethostbyname(host);
if (!hp) {
fprintf(stderr, "simplex-talk unknow host: %s/n",host);
exit(1);
}
/*build address data structure */
memset((char *)&amp;sin, 0,sizeof(sin));
sin.sin_family = AF_INET;
memcpy((char *)&amp;sin.sin_addr,hp->h_addr_list[0], hp->h_length);
sin.sin_port = htons(SERVER_PORT);
/*active open */
if ((s = socket(PF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET){
perror("simplex-talk:socket");
exit(1);
}
if (connect(s, (struct sockaddr *)&amp;sin, sizeof(sin))!=0){
perror("simplex-talk:connect");
closesocket(s);
exit(1);
}
printf("connection established!/n");
/*main loop:get and send lines of text */
while (fgets(buf, sizeof(buf), stdin)) {
buf[MAX_LINE-1] = '/0';
len = strlen(buf) +1;
if ((send(s,buf,len,0))<0) {
printf("Socket error!/n");
closesocket(s);
exit(1);
}
}
}
 
后退
顶部