A
Andrew Skouloudis
Hello all i am trying to make a C# client to work with a C++ Server .
Although the cleint is connected to the server succesfully ,i get the
following error from the Server :
Succesfully connected !!!
Could not receive .... !!
This is last error received from WSAGetLastError 10057
The error returned (10054) means :
A request to send or receive data was disallowed because the socket is
not connected and (when sending on a datagram socket using sendto) no
address was supplied. Any other type of operation might also return
this error - for example, setsockopt setting SO_KEEPALIVE if the
connection has been reset.
Can you please help me ???
C++ Server code :
#include <iostream.h>
#include <stdio.h>
#include <winsock.h>
#include <malloc.h>
int main(void)
{
int i,j;
WORD winSockVersionReq = MAKEWORD(1,1); // Getting the Winsock
version required
WSADATA wsda; // WSADATA structure to store info returned from
WSAStartup
int wsaRet; // wsaRet to get the return status from WSAStartup
int ret;
SOCKADDR_IN addr,remote_addr;
int addrLen;
SOCKET s,clientSocket;
// Initializing Winsock
wsaRet = WSAStartup(winSockVersionReq, &wsda);
if (wsaRet)
{
cout <<" FATAL: Could not initialize winsock.dll " ;
WSACleanup();
return -1;
// exit(EXIT_FAILURE);
}
if (wsda.wVersion != winSockVersionReq) // Check if the required
version match the supported
{
cout <<" Winsock version is not supported " ;
WSACleanup();
return -1;
}
s=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
// Check if the socket has been succesfully created
// You can add another test condition ??
if (s==SOCKET_ERROR)
{
cout << "FATAL : Error creating socket"<< endl;
exit(EXIT_FAILURE);
}
addr.sin_family = AF_INET;
addr.sin_port = htons(10000);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
ret = bind(s, (struct sockaddr *) &addr, sizeof(addr));
if(ret == SOCKET_ERROR)
{
cout << "Error occured from bind !!! \n";
WSACleanup();
return -1;
}
ret = listen(s,SOMAXCONN);
if(ret == SOCKET_ERROR)
{
cout << "Error occured from listen !!!! \n";
return -1;
}
addrLen=sizeof(remote_addr);
clientSocket = accept(s,(struct sockaddr *) &remote_addr,
&addrLen);
if(clientSocket == SOCKET_ERROR)
{
cout << "Error occured from accept !!! \n";
return -1;
}
cout << "Succesfully connected !!! \n";
char *buffer = new char[1000];
if (buffer == NULL)
{
cout << "Could not allocate memory for operation \n";
exit(EXIT_FAILURE);
}
ret=recv(s,buffer,1000,0);
if(ret == SOCKET_ERROR)
{
cout << "Could not receive .... !! \n " ;
cout << "This is last error received from WSAGetLastError " <<
WSAGetLastError() << endl;
exit(EXIT_FAILURE);
}
return 0;
}
C# client code :
using System;
using System.Net.Sockets;
using System.IO;
class MainClass
{
public static void Main(string[] args)
{
Console.WriteLine("--------------------------------");
Console.WriteLine("- Simple Client for C++ Server -");
Console.WriteLine("--------------------------------");
TcpClient WNMPClient;
string s,q;
WNMPClient = new TcpClient("127.0.0.1",10000);
Console.WriteLine("Please give me the message you want to send ..
");
s=Console.ReadLine();
NetworkStream networkStream ;
StreamReader streamReader ;
StreamWriter streamWriter ;
networkStream =WNMPClient.GetStream();
streamReader = new StreamReader(networkStream);
streamWriter = new StreamWriter(networkStream);
q=Console.ReadLine();
streamWriter.WriteLine(q);
streamWriter.Flush();
}
}
Although the cleint is connected to the server succesfully ,i get the
following error from the Server :
Succesfully connected !!!
Could not receive .... !!
This is last error received from WSAGetLastError 10057
The error returned (10054) means :
A request to send or receive data was disallowed because the socket is
not connected and (when sending on a datagram socket using sendto) no
address was supplied. Any other type of operation might also return
this error - for example, setsockopt setting SO_KEEPALIVE if the
connection has been reset.
Can you please help me ???
C++ Server code :
#include <iostream.h>
#include <stdio.h>
#include <winsock.h>
#include <malloc.h>
int main(void)
{
int i,j;
WORD winSockVersionReq = MAKEWORD(1,1); // Getting the Winsock
version required
WSADATA wsda; // WSADATA structure to store info returned from
WSAStartup
int wsaRet; // wsaRet to get the return status from WSAStartup
int ret;
SOCKADDR_IN addr,remote_addr;
int addrLen;
SOCKET s,clientSocket;
// Initializing Winsock
wsaRet = WSAStartup(winSockVersionReq, &wsda);
if (wsaRet)
{
cout <<" FATAL: Could not initialize winsock.dll " ;
WSACleanup();
return -1;
// exit(EXIT_FAILURE);
}
if (wsda.wVersion != winSockVersionReq) // Check if the required
version match the supported
{
cout <<" Winsock version is not supported " ;
WSACleanup();
return -1;
}
s=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
// Check if the socket has been succesfully created
// You can add another test condition ??
if (s==SOCKET_ERROR)
{
cout << "FATAL : Error creating socket"<< endl;
exit(EXIT_FAILURE);
}
addr.sin_family = AF_INET;
addr.sin_port = htons(10000);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
ret = bind(s, (struct sockaddr *) &addr, sizeof(addr));
if(ret == SOCKET_ERROR)
{
cout << "Error occured from bind !!! \n";
WSACleanup();
return -1;
}
ret = listen(s,SOMAXCONN);
if(ret == SOCKET_ERROR)
{
cout << "Error occured from listen !!!! \n";
return -1;
}
addrLen=sizeof(remote_addr);
clientSocket = accept(s,(struct sockaddr *) &remote_addr,
&addrLen);
if(clientSocket == SOCKET_ERROR)
{
cout << "Error occured from accept !!! \n";
return -1;
}
cout << "Succesfully connected !!! \n";
char *buffer = new char[1000];
if (buffer == NULL)
{
cout << "Could not allocate memory for operation \n";
exit(EXIT_FAILURE);
}
ret=recv(s,buffer,1000,0);
if(ret == SOCKET_ERROR)
{
cout << "Could not receive .... !! \n " ;
cout << "This is last error received from WSAGetLastError " <<
WSAGetLastError() << endl;
exit(EXIT_FAILURE);
}
return 0;
}
C# client code :
using System;
using System.Net.Sockets;
using System.IO;
class MainClass
{
public static void Main(string[] args)
{
Console.WriteLine("--------------------------------");
Console.WriteLine("- Simple Client for C++ Server -");
Console.WriteLine("--------------------------------");
TcpClient WNMPClient;
string s,q;
WNMPClient = new TcpClient("127.0.0.1",10000);
Console.WriteLine("Please give me the message you want to send ..
");
s=Console.ReadLine();
NetworkStream networkStream ;
StreamReader streamReader ;
StreamWriter streamWriter ;
networkStream =WNMPClient.GetStream();
streamReader = new StreamReader(networkStream);
streamWriter = new StreamWriter(networkStream);
q=Console.ReadLine();
streamWriter.WriteLine(q);
streamWriter.Flush();
}
}