[英]JAVA - Sockets :: readline() not doing anything
I created a Network-class where I open a socket an than send requests to it. 我创建了一个网络类,在其中打开一个套接字,然后向其发送请求。
The class: 班级:
import java.io.*;
import java.net.*;
public class Network implements Runnable {
private boolean isHost = false;
private int hostPort = 19250;
private int clientPort = 19251;
private String host = "127.0.0.1";
private String name = "";
private ServerSocket ownServer = null;
private Socket otherServer = null;
private DataOutputStream outToServer = null;
private BufferedReader inFromServer = null;
private boolean isConnected = false;
/**
* Konstruktor -
*/
Network(String name) {
this.name = name;
this.findHost();
// if no host exists, become host itself
if (this.isHost == false) {
// host da -> become client
System.out.println(this.name + ": host found");
try {
ownServer = new ServerSocket(this.clientPort);
} catch (Exception e) {
e.printStackTrace();
}
} else {
// kein host da -> become host
System.out.println(this.name + ": no host found");
try {
ownServer = new ServerSocket(this.hostPort);
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* connect() - versucht mit anderem server zu connecten fals dies noch nicht
* geschehen ist
*
* @return true/false
*/
public boolean connect() {
if (this.isConnected == false) {
try {
if (this.isHost) {
otherServer = new Socket(this.host, this.clientPort);
} else {
otherServer = new Socket(this.host, this.hostPort);
}
} catch (IOException e) {
//System.out.println(this.name + ": connect failed");
this.isConnected = false;
return false;
}
}
try {
outToServer = new DataOutputStream(otherServer.getOutputStream());
inFromServer = new BufferedReader(new InputStreamReader(
otherServer.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
//System.out.println(this.name + ": connect sucess");
this.isConnected = true;
return true;
}
/**
* findHost() - schaut ob ein Host existiert
*
* @return true/false
*/
private boolean findHost() {
Socket hostSocket;
try {
hostSocket = new Socket(this.host, this.getHostPort());
DataOutputStream outToServer = new DataOutputStream(
hostSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(
new InputStreamReader(hostSocket.getInputStream()));
outToServer.writeBytes("uHost?\n");// Fragen ob Host ist
} catch (Exception e) {
this.isHost = true;
return false; // Keine Connection aufgebaut => kein Host vorhanden
}
this.isHost = false;
return true; // Es gibt schon einen Host
}
public int getClientPort() {
return this.clientPort;
}
public int getHostPort() {
return this.hostPort;
}
public boolean getIsHost() {
return this.isHost;
}
/**
* sendToOther() - schickt daten an anderen Server
*
* @param data
*/
public void sendToOther(String data) {
this.connect();
try {
this.outToServer.writeBytes(data);
this.outToServer.writeBytes("\n"); //new Line
this.outToServer.flush();
System.out.println(this.name + ": " + data
+ " :: send to other server");
} catch (IOException e) {
e.printStackTrace();
}
try {
this.outToServer.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void run() {
// listen to income
String line = "";
try {
while (true) {
System.out.println(this.name + ":111");
while ((line = inFromServer.readLine())!= null) {
System.out.println(this.name + ":222");
System.out.println(this.name + ":recieved: " + line);
}
System.out.println(this.name + ":333");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void readAndDo(String data) {
System.out.println(this.name + ": data recieved: " + data);
}
}
The main Method: 主要方法:
public class Application {
public static void main(String[] args) {
Network t1net=new Network("ss1");
Network t2net=new Network("ss2");
t1net.connect();
t2net.connect();
t1net.sendToOther("some text1");
t2net.sendToOther("sometext2");
Thread t1 = new Thread( t1net );
t1.start();
Thread t2 = new Thread( t2net );
t2.start();
}
}
the console: 控制台:
ss1: no host found
ss2: host found
ss1: some text1 :: send to other server
ss2: sometext2 :: send to other server
ss2:111
ss1:111
netstat -a: netstat -a:
TCP 127.0.0.1:19250 49on41PCX:54538 HERGESTELLT
TCP 127.0.0.1:19250 49on41PCX:54540 HERGESTELLT
TCP 127.0.0.1:19251 49on41PCX:54539 HERGESTELLT
TCP 127.0.0.1:49156 49on41PCX:0 ABHÖREN
TCP 127.0.0.1:54538 49on41PCX:19250 HERGESTELLT
TCP 127.0.0.1:54539 49on41PCX:19251 HERGESTELLT
TCP 127.0.0.1:54540 49on41PCX:19250 HERGESTELLT
The problem is, that the Network.run() block in the readline()-command. 问题是readline()命令中的Network.run()块。 The data is send, bu never recieved.
数据已发送,但从未收到。
Thank you for helping. 感谢您的帮助。
You have used the sockets in a wrong manner. 您以错误的方式使用了插槽。
ss : ServerSocket ss:ServerSocket
s : Socket s:插座
You have first made two ss
on two threads and made two s
connecting to the the intial ss
. 首先,您在两个线程上创建了两个
ss
,并创建了两个与初始ss
连接的s
。 In this way ss1
is bound to s2
and ss2
is bound to s1
. 这样,
ss1
绑定到s2
而ss2
绑定到s1
。 And you have implemented input and output only from s1
and s2
. 并且您仅实现了
s1
和s2
输入和输出。 So if you write from s1
, it won't go into s2
. 因此,如果您从
s1
写,它将不会进入s2
。 In fact you need to create another socket from ss2
that will accept connections. 实际上,您需要从
ss2
创建另一个接受连接的套接字。 And the same with ss1
. 与
ss1
相同。 I hope you get my point 希望你明白我的意思
Actually you should have created only one ss
for the host. 实际上,您应该只为主机创建一个
ss
。 Then: 然后:
socketHost = ss.accept();
socketHost = ss.accept();
socketClient = new Socket(host, port);
socketClient =新的Socket(主机,端口);
Now you can use these two sockets for communication with each other by using their I/O streams. 现在,您可以使用这两个套接字通过它们的I / O流相互通信。
In short earlier you had made two connections say a
and b
. 简而言之,您已经建立了两个连接,分别是
a
和b
。 You sent and received from a
on one thread and sent and received data from b
on the second thread. 您在一个线程上从
a
发送和接收,在第二个线程上从b
发送和接收数据。
Just look up on some tutorial of simple socket I/O and you will understand. 只要看一些简单的套接字I / O教程,您就会明白。
EDIT : See EchoClient.java and EchoServer.java from http://docs.oracle.com/javase/tutorial/networking/sockets/readingWriting.html 编辑 :从http://docs.oracle.com/javase/tutorial/networking/sockets/readingWriting.html参见EchoClient.java和EchoServer.java
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.