TCP server in C client in java comunication

2019-08-01 23:06发布

问题:

I have server in c and client in java but it doesn't work.
1. run server, server wait for clients
2. run client - client sends string
3. server get the firs character and increment it and send to client //i expect this but
server gets some wrong char
4.client get character and write it on screen //expect this, but client fail with this
exception:

Exception in thread "main" java.net.SocketException: Connection reset
        at java.net.SocketInputStream.read(SocketInputStream.java:168)
        at java.io.ObjectInputStream$PeekInputStream.read(ObjectInputStream.java:2266)
        at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2279)
        at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2750)
        at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:780)
        at java.io.ObjectInputStream.<init>(ObjectInputStream.java:280)
        at clientTCP.main(clientTCP.java:19)

5) client should end and server continue running // but client went down after the previous exception

Does anybody know where could be the problem? server, client uses TCP protocol.

server:

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <sys/un.h>
#include <unistd.h>
#include <netinet/in.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>

int main()
{
    int server_sockfd, client_sockfd;
    int server_len, client_len;
    struct sockaddr_in server_address;
    struct sockaddr_in client_address;


    server_sockfd = socket( AF_INET, SOCK_STREAM, 0 );
    server_address.sin_family = AF_INET;
    server_address.sin_addr.s_addr = inet_addr( "127.0.0.1" );
    server_address.sin_port = htons( 10000 );

    server_len = sizeof( server_address );

        if( bind( server_sockfd, ( struct sockaddr *)&server_address, server_len ) != 0 )
        {
                perror("oops: server-tcp-single");
                exit( 1 );
        }

    listen( server_sockfd, 5 );

    signal( SIGCHLD, SIG_IGN );

    while( 1 )
    {
        char ch;
        printf( "server wait...\n" );

        client_len = sizeof( client_address );
        client_sockfd = accept( server_sockfd, ( struct sockaddr *)&client_address, &client_len );

        printf( "Client connected \n" );

        if( fork() == 0 )
        {
            read( client_sockfd, &ch, 1 );
            printf( "Client send = %c\n", ch );

            ch++;

            sleep( 5 );

            printf( "Server send = %c\n", ch );
            write( client_sockfd, &ch, 1 );
            close( client_sockfd );
            exit (0 );
        }
        else
            close( client_sockfd );

    }
}

client in java:

import java.io.*;
import java.net.*;

class clientTCP
{
 public static void main(String argv[]) throws Exception
 {
  String sentence;
  String modifiedSentence;
  Socket socket = new Socket("127.0.0.1", 10000);
  InetAddress adresa = socket.getInetAddress(); //address
  System.out.print("Connecting on : "+adresa.getHostAddress()+" with hostname : "+adresa.getHostName()+"\n" );


  ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                                oos.writeObject("HalloXXXX");


                                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                                String message = (String) ois.readObject();
                                System.out.println("Message Received: " + message);
                                ois.close();
                                oos.close();
                                socket.close();
 }
} 

回答1:

You can only use ObjectOutputStream with other Java programs which also use ObjectInputStream to read it. It is hard to imagine a C program supporting Java's Serialization format.

If you want to write text I suggest you use PrintWriter/BufferedReader or a better solution is to use BufferedOutputStream and do your own encoding (so you see all errors.



回答2:

And u should use oos.flush() after the writeObject() method.