drewdaman
January 4th, 2005, 03:35 PM
hello everyone
i'm having a little trouble here. i have to modify my code (which works perfectly in my original setup) to do something ... it has to be modified for a demo and i just have to keep things simple for this.
when i tried to modify, i am getting error 10054 which is:
Connection reset by peer.
An existing connection was forcibly closed by the remote host. This normally results if the peer application on the remote host is suddenly stopped, the host is rebooted, the host or remote network interface is disabled, or the remote host uses a hard close (see setsockopt for more information on the SO_LINGER option on the remote socket). This error may also result if a connection was broken due to keep-alive activity detecting a failure while one or more operations are in progress. Operations that were in progress fail with WSAENETRESET. Subsequent operations fail with WSAECONNRESET.
I don't really see how this could be happening from my code! when i run the code, the client exits with error code 10054.
i create an instance of the client and server and call tcpinitial on both (of course in separate projects). Everything works except for the receive in the client. This should become clear if you take a look at the code below. That is what is causing the problem. The error occurs at the line
nret=recv(tcpSocket, msg, 100,0);
of the TCPinitial function in the client. I have marked the line in the code as well, you can't miss it! :)
A call to WSAGetLastError after the above line gives error 10054.
relevant code for server:
Server::Server(){
nLen = sizeof(SOCKADDR);
channel=0;
buffSize=1000;
curFileName="long.txt";
int nret;
WORD wVersionRequested = MAKEWORD(1,1);
WSADATA wsaData;
int nRet;
nRet = WSAStartup(wVersionRequested, &wsaData);
if (wsaData.wVersion != wVersionRequested)
{
fprintf(stderr,"\n Wrong version\n");
return;
}
listeningSocket = socket(AF_INET, // Go over TCP/IP
SOCK_STREAM, // This is a stream-oriented socket
IPPROTO_TCP); // Use TCP rather than UDP
serverInfo.sin_family = AF_INET;
serverInfo.sin_addr.s_addr = INADDR_ANY; // Since this socket is listeningfor connections,
// any local address will do
serverInfo.sin_port = htons(nPort); // Convert integer 8888 tonetwork-byte order
// and insert into the port field
// Bind the socket to our local server address
nret = bind(listeningSocket, (LPSOCKADDR)&serverInfo, sizeof(struct
sockaddr));
// Fill in the address structure
//
saServer.sin_family = AF_INET;
saServer.sin_addr.s_addr = INADDR_ANY; // Let WinSock assign address
saServer.sin_port = htons(nPort); // Use port passed from user
}//end constructor
int Server::sendTCPinitial(){
int chan; //variable to hold the channel number sent by the client.
int nret;
char *incoming = new char[1];
char * outgoing=new char[50];
// Make the socket listen
nret = listen(listeningSocket, 10); // Up to 10 connections may wait at any
// one time to be accept()'ed
// Wait for a client
theClient = accept(listeningSocket,
NULL, // Address of a sockaddr structure (see explanation below)
NULL); // Address of a variable containing size of sockaddr struct
//receive the channel number
recv(theClient, incoming, 1, 0);
chan=atoi(incoming);
//send message to client.
//cout<<"channel is:" <<chan<<endl;
outgoing=getTCPmessage (getFileSize(curFileName));
// cout<<endl<<"outgoing is: " <<strlen(outgoing)<<endl;
send(theClient, outgoing, strlen(outgoing), 0);
return chan;
}
relevant code for client:
//constructor
Client::Client(){
channel=0;
buffSize=0;
curFileSize=0;
WORD wVersionRequested = MAKEWORD(1,1);
WSADATA wsaData;
int nRet;
// Initialize WinSock and check the version
nRet = WSAStartup(wVersionRequested, &wsaData);
if (wsaData.wVersion != wVersionRequested)
{
fprintf(stderr,"\n Wrong version\n");
return;
}
tcpSocket = socket(AF_INET, // Go over TCP/IP
SOCK_STREAM, // This is a stream-oriented socket
IPPROTO_TCP); // Use TCP rather than UDP
serverInfo.sin_family = AF_INET;
serverInfo.sin_addr.s_addr = inet_addr("10.0.0.76");
serverInfo.sin_port = htons(nPort); // Change to network-byte order and
// insert into port field
// Fill in the address structure for the server
saServer.sin_family = AF_INET;
//saServer.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
// ^ Server's address
saServer.sin_addr.s_addr = inet_addr("10.0.0.76");
saServer.sin_port = htons(nPort); // Port number from command line
} //end contructor
void Client::TCPinitial(int ch){
char *channel=new char[1];
itoa(ch,channel,10);
int nret;
char * msg=new char[100];//="This is the client";
int l=strlen(channel);
// Create the socket
nret = connect(tcpSocket,
(LPSOCKADDR)&serverInfo,
sizeof(struct sockaddr));
/* if (nret == SOCKET_ERROR)
{
PRINTERROR("connect()");
closesocket(tcpSocket);
return;
}*/
//Send channel number
nret=send (tcpSocket, channel, l, 0);
if (nret == SOCKET_ERROR)
{
PRINTERROR("send()");
closesocket(tcpSocket);
return;
}
//int brec;
//**********************ERROR HAPPENS HERE*********************
nret=recv(tcpSocket, msg, 100,0);
cout<<WSAGetLastError(); //GIVES ERROR 10054
//*************************************************************
if (nret == SOCKET_ERROR)
{
PRINTERROR("recv()");
}
}
Hoping someone can help me out here! i'm a little confused! thanks!
i'm having a little trouble here. i have to modify my code (which works perfectly in my original setup) to do something ... it has to be modified for a demo and i just have to keep things simple for this.
when i tried to modify, i am getting error 10054 which is:
Connection reset by peer.
An existing connection was forcibly closed by the remote host. This normally results if the peer application on the remote host is suddenly stopped, the host is rebooted, the host or remote network interface is disabled, or the remote host uses a hard close (see setsockopt for more information on the SO_LINGER option on the remote socket). This error may also result if a connection was broken due to keep-alive activity detecting a failure while one or more operations are in progress. Operations that were in progress fail with WSAENETRESET. Subsequent operations fail with WSAECONNRESET.
I don't really see how this could be happening from my code! when i run the code, the client exits with error code 10054.
i create an instance of the client and server and call tcpinitial on both (of course in separate projects). Everything works except for the receive in the client. This should become clear if you take a look at the code below. That is what is causing the problem. The error occurs at the line
nret=recv(tcpSocket, msg, 100,0);
of the TCPinitial function in the client. I have marked the line in the code as well, you can't miss it! :)
A call to WSAGetLastError after the above line gives error 10054.
relevant code for server:
Server::Server(){
nLen = sizeof(SOCKADDR);
channel=0;
buffSize=1000;
curFileName="long.txt";
int nret;
WORD wVersionRequested = MAKEWORD(1,1);
WSADATA wsaData;
int nRet;
nRet = WSAStartup(wVersionRequested, &wsaData);
if (wsaData.wVersion != wVersionRequested)
{
fprintf(stderr,"\n Wrong version\n");
return;
}
listeningSocket = socket(AF_INET, // Go over TCP/IP
SOCK_STREAM, // This is a stream-oriented socket
IPPROTO_TCP); // Use TCP rather than UDP
serverInfo.sin_family = AF_INET;
serverInfo.sin_addr.s_addr = INADDR_ANY; // Since this socket is listeningfor connections,
// any local address will do
serverInfo.sin_port = htons(nPort); // Convert integer 8888 tonetwork-byte order
// and insert into the port field
// Bind the socket to our local server address
nret = bind(listeningSocket, (LPSOCKADDR)&serverInfo, sizeof(struct
sockaddr));
// Fill in the address structure
//
saServer.sin_family = AF_INET;
saServer.sin_addr.s_addr = INADDR_ANY; // Let WinSock assign address
saServer.sin_port = htons(nPort); // Use port passed from user
}//end constructor
int Server::sendTCPinitial(){
int chan; //variable to hold the channel number sent by the client.
int nret;
char *incoming = new char[1];
char * outgoing=new char[50];
// Make the socket listen
nret = listen(listeningSocket, 10); // Up to 10 connections may wait at any
// one time to be accept()'ed
// Wait for a client
theClient = accept(listeningSocket,
NULL, // Address of a sockaddr structure (see explanation below)
NULL); // Address of a variable containing size of sockaddr struct
//receive the channel number
recv(theClient, incoming, 1, 0);
chan=atoi(incoming);
//send message to client.
//cout<<"channel is:" <<chan<<endl;
outgoing=getTCPmessage (getFileSize(curFileName));
// cout<<endl<<"outgoing is: " <<strlen(outgoing)<<endl;
send(theClient, outgoing, strlen(outgoing), 0);
return chan;
}
relevant code for client:
//constructor
Client::Client(){
channel=0;
buffSize=0;
curFileSize=0;
WORD wVersionRequested = MAKEWORD(1,1);
WSADATA wsaData;
int nRet;
// Initialize WinSock and check the version
nRet = WSAStartup(wVersionRequested, &wsaData);
if (wsaData.wVersion != wVersionRequested)
{
fprintf(stderr,"\n Wrong version\n");
return;
}
tcpSocket = socket(AF_INET, // Go over TCP/IP
SOCK_STREAM, // This is a stream-oriented socket
IPPROTO_TCP); // Use TCP rather than UDP
serverInfo.sin_family = AF_INET;
serverInfo.sin_addr.s_addr = inet_addr("10.0.0.76");
serverInfo.sin_port = htons(nPort); // Change to network-byte order and
// insert into port field
// Fill in the address structure for the server
saServer.sin_family = AF_INET;
//saServer.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
// ^ Server's address
saServer.sin_addr.s_addr = inet_addr("10.0.0.76");
saServer.sin_port = htons(nPort); // Port number from command line
} //end contructor
void Client::TCPinitial(int ch){
char *channel=new char[1];
itoa(ch,channel,10);
int nret;
char * msg=new char[100];//="This is the client";
int l=strlen(channel);
// Create the socket
nret = connect(tcpSocket,
(LPSOCKADDR)&serverInfo,
sizeof(struct sockaddr));
/* if (nret == SOCKET_ERROR)
{
PRINTERROR("connect()");
closesocket(tcpSocket);
return;
}*/
//Send channel number
nret=send (tcpSocket, channel, l, 0);
if (nret == SOCKET_ERROR)
{
PRINTERROR("send()");
closesocket(tcpSocket);
return;
}
//int brec;
//**********************ERROR HAPPENS HERE*********************
nret=recv(tcpSocket, msg, 100,0);
cout<<WSAGetLastError(); //GIVES ERROR 10054
//*************************************************************
if (nret == SOCKET_ERROR)
{
PRINTERROR("recv()");
}
}
Hoping someone can help me out here! i'm a little confused! thanks!