java通信中C客户端的TCP服务器

发布于 2024-12-26 02:48:41 字数 3538 浏览 0 评论 0原文

我有 c 语言的服务器和 java 语言的客户端,但它不起作用。
1. 运行服务器,服务器等待客户端
2. 运行客户端 - 客户端发送字符串
3.服务器获取第一个字符并递增它并发送给客户端//我希望如此,但是
服务器得到一些错误的字符
4.客户端获取字符并将其写在屏幕上 //期望如此,但是客户端因此
异常而失败:

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)客户端应该结束并且服务器继续运行 //但是客户端在上一个异常之后停止了

有人知道吗问题可能出在哪里?服务器、客户端使用TCP协议。

服务器:

#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 );

    }
}

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美胚控场 2025-01-02 02:48:42

您只能将 ObjectOutputStream 与其他也使用 ObjectInputStream 读取它的 Java 程序一起使用。很难想象 C 程序支持 Java 的序列化格式。

如果您想编写文本,我建议您使用 PrintWriter/BufferedReader 或更好的解决方案是使用 BufferedOutputStream 并进行自己的编码(这样您就可以看到所有错误。

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.

仅此而已 2025-01-02 02:48:42

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文