简体   繁体   English

Java通讯中C客户端中的TCP服务器

[英]TCP server in C client in java comunication

I have server in c and client in java but it doesn't work. 我有c中的服务器和java中的客户端,但它不起作用。
1. run server, server wait for clients 1.运行服务器,服务器等待客户端
2. run client - client sends string 2.运行客户端-客户端发送字符串
3. server get the firs character and increment it and send to client //i expect this but 3.服务器获取第一个字符并增加它并发送给客户端//我期待这个但是
server gets some wrong char 服务器得到一些错误的char
4.client get character and write it on screen //expect this, but client fail with this 4.客户端获取字符并将其写在屏幕上///对此进行预期,但是客户端由此失败
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 5)客户端应该结束并且服务器继续运行//但是客户端在上一个异常之后停止运行

Does anybody know where could be the problem? 有谁知道问题出在哪里? server, client uses TCP protocol. 服务器,客户端使用TCP协议。

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: 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();
 }
} 

You can only use ObjectOutputStream with other Java programs which also use ObjectInputStream to read it. 您只能将ObjectOutputStream与其他使用ObjectInputStream读取它的Java程序一起使用。 It is hard to imagine a C program supporting Java's Serialization format. 很难想象有一个支持Java的序列化格式的C程序。

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. 如果你想写文字我建议你使用PrintWriter / BufferedReader或更好的解决方案是使用BufferedOutputStream并做你自己的编码(所以你看到所有的错误。

你应该在writeObject()方法之后使用oos.flush()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM