Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C client and Java server 1

Status
Not open for further replies.

GuzaPasha

Technical User
May 15, 2003
311
FR
Hi all,

I am having troubles. I made Java Server that listens on specified port. I wanna C client to connect to this server. Server can receice desired data from client, but when send back data, client cannot get it. Client is blocked somehow.

Doing this all separately is ok. For example.
Client is able to receive data, but just if I use recv funciton alone(in C). Client is also able to send everythig to the server correctly,bu just if I use send function alone in the Client.

this combination
send(...);
recv(...);

doesn't work at all

Any idea is helpful
GuzaPasha
 
Just a guess, but are you allowing time for the server to respond?

send();recv(); takes almost no time at all.

Sending the message over the wire, receiving it at the other end, being read by the server, the server doing whatever it does with the message and getting the response back takes way longer.

--
 
here is the simple client code

strcpy(out_buf, "1234567890");
rc = send(client_s, out_buf, sizeof(out_buf)+1, 0);
if (rc<0){
printf("Error sending data \n");
closesocket(client_s);
exit(1);
}
// Receive from the server

rc = recv(client_s, in_buf, sizeof(in_buf), 0);
if (rc<0){
printf("Error receiving data \n");
closesocket(client_s);
exit(1);
}
else printf("Received from server... data = '%s' \n", in_buf);

Should the client wait for the server to respond, recv function should wait for response from the server!??

here is the simple java server

ServerSocket server =
new ServerSocket(port);
System.out.println("Started on port " + port);

while (true ){
Socket client = server.accept();
InputStream input = client.getInputStream();
OutputStream output = client.getOutputStream();

BufferedReader reader =
new BufferedReader(
new InputStreamReader(input));
PrintWriter writer =
new PrintWriter(
new OutputStreamWriter(output),true);
//get data from client
s = reader.readLine();
System.out.println("INCOMING: " + s + "\n");
//send data back
writer.println("goodbye");
client.close();
}
Salem, can you please have a look at this all....

Client code in C using send at after that recv, doesn't send anything to the server. When I comment steps 'recv', server gets the info from client. I am not able to receive anything using recv after send command. Yes, server is procesing recevied string, but recv should wait for response.

Hope that you have much more information on this.
Thanks in advance

GuzaPasha

p.s. If you are JAVA and C programmer, i can send you the client.c and server.java code, to try it.
 
> rc = send(client_s, out_buf, sizeof(out_buf)+1, 0);
Depending on how you declared out_buf (and in_buf later on), this is almost certainly not what you want.

If you have [tt]char out_buf[100];[/tt] for example, then your message will contain 90 bytes of junk along with the 10 bytes of useful data.

If on the other hand you have [tt]char *out_buf;[/tt], then assuming you have even allocated some memory, your sizeof(out_buf) is pretty much going to be just 4 (assuming a fairly usual 32 bit environment).

Try for example
Code:
char out_buf[100];
// some other code
rc = send(client_s, out_buf, strlen(out_buf)+1, 0);

> rc = recv(client_s, in_buf, sizeof(in_buf), 0);
Two things you should note.
1. If the server sends say 100 bytes, there is NO guarantee that (for a TCP connection) you will receive all those 100 bytes in a single recv() call. Network fragmentation and delay may mean that you may say 42 bytes on one call, and 58 bytes on the next. Packet reassembly is your task to perform in a TCP connection. Typically, some part of the stream tells you how much data to expect.

2. recv() does NOT append a \0 to the message, so immediately trying to print it with %s in printf is a bad idea.

Code:
char in_buf[100];
rc = recv(client_s, in_buf, sizeof(in_buf)-1, 0);  // allow room for the \0 we append later
if ( rc > 0 ) {
  in_buf[rc] = '\0';
  printf( "Received %s\n", in_buf );
}
Typically, recv() is put inside a while loop, which exits when some predetermined "end of stream" event happens

--
 
Salem, you are right. Thank you for this. I've realized this last night.

GuzaPasha
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top