【发布时间】:2014-08-26 23:55:32
【问题描述】:
我创建了一个网络类,我在其中打开一个套接字,然后向它发送请求。
班级:
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);
}
}
主要方法:
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();
}
}
控制台:
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:
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
问题是,Readline() 命令中的 Network.run() 块。数据已发送,但从未收到。
感谢您的帮助。
【问题讨论】:
-
它在 Network.connect() 方法中连接。或者这不是你的意思吗?
-
你应该从分离客户端和服务器开始,这不是很可读。我猜您在客户端上写入字符串后只是缺少
flush。 -
在这种情况下,分隔不是一个选项。写入字符串后直接在 Network.sendToOther() 中调用刷新。
-
@user3443137 不,不是,您在
findHost方法中本地声明了outToServer变量。没有办法从其他方法中清除它。由于您是 Pokemon 捕获所有异常,因此您看不到NullPointerException。 -
findHost() 方法在连接中没有做任何有用的事情。它仅用于查找主机是否存在。那里不处理连接。 |捕获所有异常还包括 NullPointers,所以应该没有问题
标签: java sockets tcp readline serversocket