Java服务器线程和C客户端套接字连接

时间:2016-04-09 10:38:04

标签: java c

我尝试将文本从C客户端发送到Java订阅者列表(在不同端口上侦听)。

为此,我滚动订阅者列表,建立与不同订阅者的连接并将文本发送给他们。

但是,与订户的连接失败。

JAVA SERVER THREAD:

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

1 个答案:

答案 0 :(得分:0)

我已经解决了改变两件事的问题:

subscriber.sin_port = htons(s->address.sin_port);
  1. 我需要添加 htons ,因为地址存储的格式为“从网络到主机”(ntohs)。
  2. 最后,我忘了关闭连接