gpt4 book ai didi

Java服务器线程与C客户端socket连接

转载 作者:行者123 更新时间:2023-11-30 17:02:38 25 4
gpt4 key购买 nike

我正在尝试将文本从 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. 最后,我忘记关闭连接

关于Java服务器线程与C客户端socket连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36515654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com