Alright. I’ll put some effort into this. Kind of a refreshing thing to do. ^^
I modified your code, so the message length pattern is used. The c client sends a message to the server, the java client decodes it and sends it back to the client. So you have both direction of encoding up and running. Please try to understand, what is happening in the code, otherwise you will run into problems again. The ByteOrder Stuff is completly done on the java side, so you can fiddle around with your closed C server.
C Client:
int main(int argc, char* argv[])
{
WSAData version; //We need to check the version.
WORD mkword=MAKEWORD(2,2);
int what=WSAStartup(mkword,&version);
if(what!=0){
std::cout<<"This version is not supported! - \n"<<WSAGetLastError()<<std::endl;
}
else{
std::cout<<"Good - Everything fine!\n"<<std::endl;
}
SOCKET u_sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(u_sock==INVALID_SOCKET)
std::cout<<"Creating socket fail\n";
else
std::cout<<"It was okay to create the socket\n";
//Socket address information
sockaddr_in addr;
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=inet_addr("127.0.0.1");
addr.sin_port=htons(15000);
std::cout<<"Successfully provided the address"<<std::endl;
/*==========Addressing finished==========*/
//Now we connect
int conn=connect(u_sock,(SOCKADDR*)&addr,sizeof(addr));
std::cout<<"conn value:"<<conn<<std::endl;
if(conn==SOCKET_ERROR){
std::cout<<"Error - when connecting "<<WSAGetLastError()<<std::endl;
closesocket(u_sock);
WSACleanup();
}
std::cout<<"Successfully connected to server"<<std::endl;
//Send some message to remote host
char* mymsg="Hello Server...How are you?";
int length = strlen(mymsg);
//Cast the integer to char and send it
int smsg=send(u_sock,reinterpret_cast<char*>(&length), sizeof(int),0);
//Send the actual message
smsg=send(u_sock,mymsg,strlen(mymsg),0);
int newlength;
//Receive exactly 4 bytes for the length. If not the right length is received, repeat.
int get = 0;
while((get+=recv(u_sock,(reinterpret_cast<char*>(&newlength))+get,4,0)) < 4) {}
std::cout<<"Length: " << newlength << std::endl;
//Create new char array with newlength + 1 so we have a zero terminated string.
char* newMsg = new char[newlength+1];
memset(newMsg,0,newlength+1);
get = 0;
//Receive the string. If not the right length is received, repeat.
while((get+=recv(u_sock,newMsg+get,newlength,0)) < newlength) {}
std::cout<<"Message: " << newMsg << std::endl;
closesocket(u_sock);
int i;
std::cin >> i;
return 0;
}
Java Server:
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.#
int portNumber = 15000;
try (
ServerSocket serverSocket = new ServerSocket(portNumber);
Socket clientSocket = serverSocket.accept();
OutputStream os = clientSocket.getOutputStream();
PrintWriter out = new PrintWriter(os, true);
//BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
InputStream is = clientSocket.getInputStream();
) {
//RECV
//Create ByteBuffer for length integer
ByteBuffer bLength = ByteBuffer.allocate(4);
//C is usually Little_Endian
bLength.order(ByteOrder.LITTLE_ENDIAN);
//Read 4 bytes
is.read(bLength.array(), 0, 4);
//Convert the length
int length = bLength.getInt();
System.out.println("Length: "+length);
//Allocate ByteBuffer for message
ByteBuffer bMessage = ByteBuffer.allocate(length);
bMessage.order(ByteOrder.LITTLE_ENDIAN);
is.read(bMessage.array(), 0, length);
//Convert the message to string
String msg = new String( bMessage.array() );
System.out.println(msg);
//SEND
//Create ByteBuffer with length
ByteBuffer bLengthNew = ByteBuffer.allocate(4);
bLengthNew.order(ByteOrder.LITTLE_ENDIAN);
bLengthNew.putInt(msg.length());
//Write the length bytebuffer to the outputstream
os.write(bLengthNew.array());
//Write the message to the outputstream. (Don't use println)
out.print(msg);
//Flush it. (It automatically gets flushed on a \n, but we dont want that.
out.flush();
} catch (IOException e) {
System.out.println("Exception caught when trying to listen on port "
+ portNumber + " or listening for a connection");
System.out.println(e.getMessage());
}
}
4
solved C++ Client unable to communicate with Java server