I have a C++ client and a Java Server. I’m simply trying to send a string “Test” from the client. Here’s my relevant JAVA code,
Socket clientSocket = serverSocket.accept();
BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String line = "";
while((reader.readLine()) != null) {
System.out.println("Recieved Something. " + line.length());
System.out.println(line);
}
Here’s my output (There’s a blank line after Received Something)
Recieved Something. 0
Recieved Something. 0
And the C++ Code (not entirely sure what you guys need as I’m new to C++
SOCKET Socket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(Socket==INVALID_SOCKET)
{
printf("Winsock error - Socket creation Failed!\r\n");
WSACleanup();
return 0;
}
struct hostent *host;
if((host=gethostbyname("localhost"))==NULL)
{
printf("Failed to resolve hostname.\r\n");
WSACleanup();
return 0;
}
SOCKADDR_IN SockAddr;
SockAddr.sin_port= htons(2501);
SockAddr.sin_family= AF_INET;
SockAddr.sin_addr.s_addr= *((unsigned long*)host->h_addr);
if(connect(Socket,(SOCKADDR*)(&SockAddr),sizeof(SockAddr))!=0)
{
printf("Failed to establish connection with server\r\n");
WSACleanup();
return 0;
}
string toSend = "Test\n";
send(Socket,toSend.c_str(), strlen(toSend.c_str()), 0);
So why am I not receiving the Test String?
Thanks
You need to assign the line varible some value.
use