user2328999
user2328999

Reputation: 91

send an integer from a C client to a Java server

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 ...?

Upvotes: 0

Views: 2447

Answers (1)

K Scott Piel
K Scott Piel

Reputation: 4380

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.

Upvotes: 2

Related Questions