主题:关于tcp多次数据发送的问题
小弟第一次学tcp网络编程,我想通过一个循环将客户端的信息发送10遍给服务器,结果服务器只接收到一条信息,但是在服务器向客户端发送的连接信息却有10条,不知道为什么,请各位帮忙看下!
//************************客户端程序**********************//
#include <Winsock2.h>
#include <stdio.h>
void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return;
}
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 ) {
WSACleanup( );
return;
}
SOCKET sockClient=socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(6000);
for(int i=1;i<10;i++) //我的想法是通过这个for循环,将数据发送10遍
{
char recvBuf[100];
connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(sockaddr));
recv(sockClient,recvBuf,100,0); //这条接收服务器端发送信息的,在运行过程中收到10次!
printf("%s\n",recvBuf);
send(sockClient,"我的第一个",strlen("我的第一个")+1,0); //这这条信息,只给服务器发送了一条信息!
if(i>1)
{
closesocket(sockClient);
}
}
//closesocket(sockClient);
WSACleanup();
}
//*******************服务器程序*****************************//
#include <Winsock2.h>
#include <stdio.h>
void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return;
}
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 ) {
WSACleanup( );
return;
}
SOCKET sockSrv=socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(6000);
bind(sockSrv,(SOCKADDR *) &addrSrv,sizeof(SOCKADDR));
SOCKADDR_IN addrClient;
int len=sizeof(SOCKADDR);
while(1)
{
listen(sockSrv,5);
SOCKET sockConn=accept(sockSrv,(SOCKADDR *) &addrClient,&len);
char sendBuf[100];
sprintf(sendBuf,"welcome %s to here",inet_ntoa(addrClient.sin_addr));
send(sockConn,sendBuf,strlen(sendBuf)+1,0);
char recvBuf[100];
recv(sockConn,recvBuf,100,0);
printf("%s\n",recvBuf);
closesocket(sockConn);
}
}
//************************客户端程序**********************//
#include <Winsock2.h>
#include <stdio.h>
void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return;
}
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 ) {
WSACleanup( );
return;
}
SOCKET sockClient=socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(6000);
for(int i=1;i<10;i++) //我的想法是通过这个for循环,将数据发送10遍
{
char recvBuf[100];
connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(sockaddr));
recv(sockClient,recvBuf,100,0); //这条接收服务器端发送信息的,在运行过程中收到10次!
printf("%s\n",recvBuf);
send(sockClient,"我的第一个",strlen("我的第一个")+1,0); //这这条信息,只给服务器发送了一条信息!
if(i>1)
{
closesocket(sockClient);
}
}
//closesocket(sockClient);
WSACleanup();
}
//*******************服务器程序*****************************//
#include <Winsock2.h>
#include <stdio.h>
void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return;
}
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 ) {
WSACleanup( );
return;
}
SOCKET sockSrv=socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(6000);
bind(sockSrv,(SOCKADDR *) &addrSrv,sizeof(SOCKADDR));
SOCKADDR_IN addrClient;
int len=sizeof(SOCKADDR);
while(1)
{
listen(sockSrv,5);
SOCKET sockConn=accept(sockSrv,(SOCKADDR *) &addrClient,&len);
char sendBuf[100];
sprintf(sendBuf,"welcome %s to here",inet_ntoa(addrClient.sin_addr));
send(sockConn,sendBuf,strlen(sendBuf)+1,0);
char recvBuf[100];
recv(sockConn,recvBuf,100,0);
printf("%s\n",recvBuf);
closesocket(sockConn);
}
}