I am trying to write a c language server and a java client to send messages.
I am able to successfully connect the client and server. However, the c server is not able to receive the data from the client server.
The expected result, I would like to get is.
1) Start the server
2) start the client application
3) User key in some words into the textfield and click send.
4) Then the server side receive the String I send from client.
Is there some sort of encoding errors that I missed out or data mismatch? I would like to get some help on why the data is not received on the server side. Thank You!
The essential code used in my java client code are:
private Socket socket;
private static final int SERVERPORT = 4000;
private static final String SERVER_IP = "192.168.8.112";
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
socket = new Socket(serverAddr, SERVERPORT);
In the java application I have a text field for user to input data:
public void onClick(View view) {
try {
EditText et = (EditText) findViewById(R.id.EditTextT);
String str = et.getText().toString();
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
out.println(str);
}
}
In my server code, I used the code from here
#include<stdio.h>
#include<string.h> //strlen
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
#include<unistd.h> //write
int main(int argc , char *argv[])
{
int socket_desc , client_sock , c , read_size;
struct sockaddr_in server , client;
char client_message[2000];
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
puts("Socket created");
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 4000 );
//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
//print the error message
perror("bind failed. Error");
return 1;
}
puts("bind done");
//Listen
listen(socket_desc , 3);
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
//accept connection from an incoming client
client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
if (client_sock < 0)
{
perror("accept failed");
return 1;
}
puts("Connection accepted");
//Receive a message from client
while( (read_size = recv(client_sock , client_message , 2000 , 0)) > 0 )
{
//Send the message back to client
write(client_sock , client_message , strlen(client_message));
}
printf("Message received: %s\n",client_message);
if(read_size == 0)
{
puts("Client disconnected");
fflush(stdout);
}
else if(read_size == -1)
{
perror("recv failed");
}
return 0;
}
Editing Notes: I changed 8888 to 4000.
Additional Information : There is no error on compilation for both piece of code. Connection between the client and server can be established successfully. The output on the server sides print Message received:. It is empty without the word i send from the client app.
BufferedWriter. What do you know about buffering?