簡體   English   中英

Java服務器線程和C客戶端套接字連接

[英]Java server thread and C client socket connection

我正在嘗試將文本從C客戶端發送到Java訂戶列表(在不同的端口上監聽)。

為此,我滾動了訂戶列表,連接到不同的訂戶並將文本發送給他們。

但是,與訂戶的連接失敗。

JAVA服務器線程:

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

public class ServerThread extends Thread {
    private static ServerSocket _ss;
    private static DataInputStream _in = null;
    private static DataOutputStream _out = null;
    private static BufferedReader _br = null;

    private static String _topic = null;

    public ServerThread(String string, ServerSocket ss, String topic) {
        super(string);
        _ss = ss;
        _topic = topic;
    }

    public void run() {
      String text;

        try {
            System.out.println("Listening on port " + _ss.getLocalPort());

            while(true) {
                 // Waiting connections             
                Socket sc = _ss.accept();

                // Input
                _in = new DataInputStream(sc.getInputStream());
                _br = new BufferedReader(new InputStreamReader(_in));

                // Get text
                while((text = _br.readLine()) != null) {
                   System.out.println("MESSAGE FROM " + _topic + " : " + text);
                }
            }

        } catch(IOException e) {
          System.out.println(e.getMessage());
      }
    }

}

將文本發送給訂閱者:

int send_text_subscriber(char *tp, char *text) {
    struct topic *t;
    struct subscriber *s;
    int sd;
    struct sockaddr_in subscriber;

    t = find_topic(tp);

    if(t != NULL) {
        s = LIST_FIRST(&t->s_head);

        if(!LIST_EMPTY(&t->s_head)) {
            while(s != NULL) {
                /* Open socket */
                    if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
                        perror("socket");
                        return -1;
                    }

                /* Set address */
                subscriber.sin_family = AF_INET;
                subscriber.sin_addr = s->address.sin_addr;
                subscriber.sin_port = s->address.sin_port;

                /* Connect */
                    if(connect(sd, (struct sockaddr*)&subscriber, sizeof(subscriber)) == -1) {
                        printf("\n  > Error in the connection to the subscriber %s:%d\n", inet_ntoa(subscriber.sin_addr), subscriber.sin_port);
                        return -1;
                    }

                /* Send text */
                    printf("\n  > Sending text to %s:%d... ", inet_ntoa(subscriber.sin_addr), subscriber.sin_port);
                    if(write(sd, text, strlen(text)) == -1) {
                        perror("write");
                        return -1;
                    }
                    printf("[OK]\n\n");

                s = LIST_NEXT(s, entries);
            }
        } 
    }

主題訂閱成功時,我創建了服務器線程:

static int subscribe(String topic) {
        try {
            // Open connection to the broker
            _sd = new Socket(_server, _port);
            _ss = new ServerSocket(0); // Server Socket Descriptor

            _in = new DataInputStream(_sd.getInputStream());
            _out = new DataOutputStream(_sd.getOutputStream());

            // Send type operation
            _out.write(SUBSCRIBE.getBytes(), 0, SUBSCRIBE.length());
            _out.write('\0');
            _out.flush();
            // Send the topic to subscribe to
            _out.write(topic.getBytes(), 0, topic.length());
            _out.write('\0');
            _out.flush();
            // Send listening port
            _out.writeShort((short)_ss.getLocalPort());
            _out.flush();

            // Get response from the broker
            if(_in.read() == 0) {
                System.out.println("c> SUBSCRIBE OK");

                // Create server thread
                ServerThread t = new ServerThread("server", _ss, topic);
                t.start();

            } else {
                System.out.println("c> SUBSCRIBE FAIL");
            }

            // Close connection
            _in.close();
      _sd.close();

        } catch(IOException e) {
      System.out.println("Error in the connection to the broker " + _server + ":" + _port);
    }

        return 0;
    }

我已經解決了更改兩件事的問題:

subscriber.sin_port = htons(s->address.sin_port);
  1. 我需要添加htons ,因為該地址以“從網絡到主機”( ntohs )的格式存儲端口。
  2. 最后,我忘了關閉連接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM