我创建了一个简单的多线程服务器。每次它接受客户一个DataInputStream和DataOutputStream创建和交际开始多线程服务器从一个客户端接收数据JAVA
服务器:
public class Connection implements Runnable{
boolean isAlreadyOpened = false;
@Override
public void run() {
// TODO Auto-generated method stub
try {
ServerSocket ss = new ServerSocket(7000);
while(true){
System.out.println("Il Server sta cercando Connessioni");
Socket s = ss.accept();
System.out.println("Il Server ha accettato un Client");
Thread t2 = new Thread(new Runnable(){
public void run(){
try {
DataInputStream dis = new DataInputStream(s.getInputStream());
isAlreadyOpened = true;
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
while(true){
String test = dis.readUTF();
dos.writeUTF(test);
System.out.println(test);
dos.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
isAlreadyOpened = false;
}
}
});
t2.start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我已经尝试连接一个Android应用程序和JavaApp;每40秒钟,这两个程序都会通过writeUTF发送一个字符串。从两个客户端正确建立连接,但服务器仅从最后一个连接到服务器的端口接收数据。 如何允许服务器从/向所有客户端接收/发送数据?
编辑:
我已经尝试设置这样的:
public class Connection implements Runnable {
@Override
public void run() {
try {
ServerSocket ss = new ServerSocket(7000);
while (true) {
System.out.println("Server is listening");
Socket s = ss.accept();
System.out.println("Client Accepted");
Thread t2 = new Thread(new Runnable() {
public void run() {
try {
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
while (true) {
String test = dis.readUTF();
dos.writeUTF(test);
System.out.println(test);
dos.flush();
}
} catch (IOException
e.printStackTrace();
} finally {
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
t2.start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
但结果是一样的
编辑:
的Andorid客户端代码
public class Connection implements Runnable {
@Override
public void run() {
try {
Socket s = new Socket("127.0.0.1", 7000);
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
while(true){
dos.writeUTF("FromAndroid");
Log.d("InputStreammmm", dis.readUTF());
Thread.sleep(10000)
}
} catch (IOException e) {
e.printStackTrace();
}
}
Java应用程序客户端代码
@Override
public void run() {
try {
Socket socket = new Socket("127.0.0.1", 7000);
System.out.println("Connessooo");
DataInputStream dis = new DataInputStream(socket.getInputStream());
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
while(true){
dos.writeUTF("Invioooooooooooooooooooooooooo");
result = dis.readUTF();
System.out.println(result);
Thread.sleep(10000);
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
,带有表插座编辑:
public class Connection implements Runnable {
List sList = new ArrayList<>();
Socket s;
int i = 0;
@Override
public void run() {
// TODO Auto-generated method stub
try {
ServerSocket ss = new ServerSocket(7000);
while (true) {
System.out.println("Server Listening");
s = ss.accept();
sList.add(s);
System.out.println("Accepted Client --- " +s.toString());
Thread t2 = new Thread(new Runnable() {
public void run() {
try {
DataInputStream dis = new DataInputStream(s.getInputStream());
while (true)
{
String test = dis.readUTF();
System.out.println("Message sent from -- " + sList.get(i).toString());
System.out.println(test);
while(i < sList.size()){
DataOutputStream dos = new DataOutputStream(sList.get(i).getOutputStream());
dos.writeUTF(test);
System.out.println("Message Sent to -- " + sList.get(i).toString());
dos.flush();
++i;
}
i=0;
}
} catch (IOException e)
{
e.printStackTrace();
} finally
{
try
{
System.out.println("Closing Socket --- " + sList.get(i).toString());
sList.get(i).close();
sList.remove(i);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
});
t2.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
做这样的EJP的建议我的问题就解决了......
+1
你永远不会关闭接受的套接字。但否则这个代码应该工作。你确定问题不在客户端吗?但是当's'不是最终的时候,这个代码是如何编译的呢? –
+0
如果3个客户端快速连接但不立即将数据发送到服务器会怎么样?这些变量都超出了范围。当然这个代码不好。 –
+1
@ peter.petrov代码将在'readUTF()'中被阻塞,直到收到数据或发生异常为止。这里没有错。 –