0

I use this code to send an integer from my Java Client to my Java Server

int  n = rand.nextInt(50) + 1;
            DataOutputStream dos = new DataOutputStream(_socket.getOutputStream());
            dos.writeInt(n);

And i read it in the server with this code

DataInputStream din = new DataInputStream(socket.getInputStream());
        int ClientNumber= din.readInt();
        System.out.println(ClientNumber);


        ClientNumber++;
       DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
       dos.writeInt(ClientNumber);

       String randomString= getRandomValue(10,20);
       dos.writeUTF(randomString);

It work perfectly but now i want to write a C client I tried this code

  #include <stdio.h>
#include <errno.h>
#include <signal.h>

#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>

#define SERVEURNAME "localhost" // adresse IP de mon serveur

int to_server_socket = -1;

void main ( void )
{

    char *server_name = SERVEURNAME;
    struct sockaddr_in serverSockAddr;
    struct hostent *serverHostEnt;
    long hostAddr;
    long status;
    char buffer[512];

    bzero(&serverSockAddr,sizeof(serverSockAddr));
    hostAddr = inet_addr(SERVEURNAME);
    if ( (long)hostAddr != (long)-1)
        bcopy(&hostAddr,&serverSockAddr.sin_addr,sizeof(hostAddr));
    else
    {
        serverHostEnt = gethostbyname(SERVEURNAME);
        if (serverHostEnt == NULL)
        {
            printf("gethost rate\n");
            exit(0);
        }
        bcopy(serverHostEnt->h_addr,&serverSockAddr.sin_addr,serverHostEnt->h_length);
    }
    serverSockAddr.sin_port = htons(8071);
    serverSockAddr.sin_family = AF_INET;

    /* creation de la socket */
    if ( (to_server_socket = socket(AF_INET,SOCK_STREAM,0)) < 0)
    {
        printf("creation socket client ratee\n");
        exit(0);
    }
    /* requete de connexion */
    if(connect( to_server_socket,
               (struct sockaddr *)&serverSockAddr,
               sizeof(serverSockAddr)) < 0 )
    {
        printf("demande de connection ratee\n");
        exit(0);
    }
    /* envoie de donne et reception */
    int value = htons( 4 );
    write( to_server_socket, &value, sizeof( value ) );

    printf(buffer);


}

but it don't work. I use Eclipse for to compile the java code and running the Server and xcode for the C code ( the Client ) but i don't think that the problem is there

Edit: I got an error on the server

java.net.SocketException: Broken pipe at java.net.SocketOutputStream.socketWrite0(Native Method) at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92) at java.net.SocketOutputStream.write(SocketOutputStream.java:115) at java.io.DataOutputStream.writeInt(DataOutputStream.java:182) at ServiceRequest.run(ServiceRequest.java:36) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:439) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)

I think it's because i the server wait for an integer but it isn't ...?

1
  • Get the value returned by write. If -1, then check errno. Commented May 3, 2013 at 15:24

1 Answer 1

2

Two things that jump out at me in this statement...

write(to_server_socket,"4",4);

1) "4" is not an integer, it's a null terminated string (well, okay, it is an integer, but it's not what you "meant" to do me thinks)

2) You are sending the value in host-byte-order which may or may not be the same as network-byte-order

int value = htons( 4 );
write( to_server_socket, &value, sizeof( value ) );

Beyond that, however, the "broken pipe" error from the java socketWrite()would tend to indicate that your sending side (the C application) has closed the socket and your java side is still trying to write to it.

Your C client code is opening the socket, writing to it then immediately printing a buffer you never filled with anything and exiting the program. As soon as the program exits, the socket you created for it is closed, thus the "broken pipe" error in your Java server. You need to read a reply from the server...

int value = htonl( 4 );
int reply = 0;

if( send( to_server_socket, &value, sizeof( value ), 0 ) != sizeof( value ) )
{
    printf( "socket write failed: %s", strerror( errno ) );
    exit( -1 );
}

if( recv( to_server_socket, &reply, sizeof( reply ), MSG_WAITALL ) != sizeof( reply ) )
{
    printf( "socket read failed: %s", streror( errno ) );
    exit( -1 )
}

printf( "got reply: %d\n", ntohl( reply ) );

On a separate note... you indicate that you receive 262144 on the server... is that before or after the changes I suggested? 262144 is 0x00040000 -- so... you did get 4, just not where you expected to receive it. So, you're using 32 bit ints (I should have realized that) which means you want to use htonl() and ntohl() instead of the htons() and ntohs() which are short integer conversions.

Sign up to request clarification or add additional context in comments.

7 Comments

I would assume that your C application is exiting after you do the send? The broken pipe error on the write from your java app implies that the socket the C application used to send the integer isn't there anymore. I had just edited my answer to mention that after you edited the question to include the error output.
No i don't close the socket , i just have printf(buffer); to show the answer of the server. In the server i don't recive 4 but 262144
Your code is incomplete, so I can't see where you're going wrong. I can only see up until you send the int to the server, I can't see how you're getting the reply.
thank you now it work but only for int, i can't use ntohl for String ?
No... ntohl() is for converting the byte order of a 32 integer value (in this case from little-endian on an intel to big-endian network-byte-order. Strings are always sent in the same byte order. So you don't need to do that. send( socket, myString, strlen( myString )+1, 0 ) will send a complete string with the null terminator over the socket.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.