1

I want to send a number from a client to a server. My code is :

Server

n= read( client_sockfd, &choice_client, 1 ); //read client's choice printf("%d\n", choice_client);

Client

my_choice=1; //basically everything exept 0

n = write(sockfd , &my_choice, 1); //send choice to server

Outuput

0 (all times)

Arein
  • 11
  • 1
  • 2
  • You should buffer manually socket I/O. So call `read` (or `recv`) and `write` (or `send`) on large (e.g. 16 kilobytes) chunk, and have your code separate the chunks into meaningful messages. Don't expect that a `send` on one side corresponds to a single `recv` on the other side. TCP/IP sockets are just streams of bytes (*without* messages) – Basile Starynkevitch Jan 20 '15 at 10:01
  • Also, avoid when possible to `read` or `recv` a *single* byte at a time. It is *very* inefficient. As I said, you need to buffer. – Basile Starynkevitch Jan 20 '15 at 10:06
  • On which operating system? – Basile Starynkevitch Jan 20 '15 at 10:09
  • Maybe better is `write(sockfd , &my_choice, sizeof my_choice);` – i486 Jan 20 '15 at 10:11
  • Ι'm wοrking οn linux.. Αnd thank all οf yοu fοr yοur respοnds..! – Arein Jan 20 '15 at 10:24
  • it depends if you are reading and writing with in the machine or between two machines. before you call write, you should convert the integer to network endian (htons or htonl) and after you read it, you should convert the integer back to host endian (ntohs or ntohl). But this solution is not so generic as it thinks both machines have the same endian (16 bit or 32 bit). A better solution might be using the structure that contains the size of the data type( in your case its int) and the data. – John Jan 20 '15 at 10:24

3 Answers3

1

You need to set enough memory for the given type.

You are writing the integer or float variable value so you need to provide the size of integer or float.

Like

n=read(client_fds,&choice_client,sizeof(int)); //sizeof(integer or float).

Likewise you need to specify while writing

n = write(sockfd , &my_choice, sizeof(int));

If not specified the exact size then it will print default value or initialized value of that variable.

Chandru
  • 1,306
  • 13
  • 21
1

Actually you can send numbers, because both read and write takes void * as argument. You just have to pass sizeof of variable as last argument.

//Server code
n= read( client_sockfd, &choice_client, sizeof(choice_client) );        
printf("%d\n", choice_client);

//client code
my_choice=1;                                    
n = write(sockfd , &my_choice, sizeof(my_choice)); 
rejkowic
  • 253
  • 1
  • 10
0

You should define, at least in well written comments, the protocol between client and server (which messages are sent, how they are delimited) It is usally easier to debug a textual message protocol. So you might consider sending lines each terminated by a newline character (or have some more complex messages structured with a header and a body, the header giving the size of the message body. Look into HTTP or SMTP for inspiration).

You might for instance decide that each message holds a JSON encoded data and is newline terminated. Beware of endianness issues (the server and client are often different machines). See byteorder(3) (hence, sending a number as text is simpler).

Transmission time is much longer than CPU time for encoding in text format.

As I commented, you should buffer manually socket I/O. So call read (or recv) and write (or send) on large (e.g. 16 kilobytes) chunk, and have your code separate the chunks into meaningful messages. Don't expect that a send (or write) on one side corresponds to a single recv (or read) on the other side. TCP/IP sockets are just streams of bytes (without message boundaries). You should use the result of read (or recv) or write (or send) and implement buffering accordingly.

Avoid send-ing (or write-ing) short messages (e.g. one or a few bytes each). This is generally very inefficient.

Read wikipage on TCP and read(2), recv(2), write(2), send(2), socket(7), ip(7), tcp(7) etc... man pages.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547