[英]Connecting multi threads to the local host at the same time?
I'm trying to make 2 or more threads write through an output stream to a server-side application at the same time through java sockets. 我试图让两个或更多线程通过java套接字同时通过输出流写入服务器端应用程序。
but instead they are writing in a sequential order.. 但相反,他们是按顺序写的..
This MVCE acts the same.. 这个MVCE行为相同..
SERVER LISTENER 服务器听众
public static void main(String[] args) {
Socket socket=null;
try{
ServerSocket serverSocket = new ServerSocket(5050);
while (true) {
logger.warn("listening");
socket = serverSocket.accept();
new TestingSession(socket).start();
}
} catch(Exception e) {}
}
SERVER THREAD: 服务器线程:
class TestingSession extends Thread {
private static final Logger logger = Logger.getLogger(Class.class.getName());
private Socket socket;
TestingSession(Socket socket) {
try {
DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
this.socket = socket;
while (!socket.isClosed()) {
System.out.println(dataInputStream.readUTF());
}
} catch (Exception e) {
}
}
}
CLIENT MAIN 客户主要
public class Run {
public static void main(String[] args) {
UpdateTest t1 = new UpdateTest();
UpdateTest t2 = new UpdateTest();
t1.start();
t2.start();
System.out.println("finished creation");
}
}
CLIENT THREAD 客户线程
public class UpdateTest extends Thread {
public void run() {
try {
Socket socket;
socket = new Socket("127.0.0.1", 5050);
DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
for(int i=0; i <100;i++){
Thread.sleep(50);
dataOutputStream.writeUTF(""+i);
System.out.println(i);
}
}catch(Exception e){
System.out.println(e.getMessage());
}
}}
Client's output : 客户的输出:
Finished creation
完成创作
1 1 2 2 3 3 4 4 5 5
1 1 2 2 3 3 4 4 5 5
Servers output : 服务器输出:
1 2 3 4 5 6 ... 97 99 100
1 2 3 4 5 6 ... 97 99 100
-- exception socket closed.--
- 异常插座关闭.--
listening
听
1 2 3 4 5 6 .. 99 100
1 2 3 4 5 6 .. 99 100
I think you may have just messed up the class definition. 我想你可能搞砸了类的定义。 My IDE tells me that the instance variable
socket
in TestingSession
is not used. 我的IDE告诉我,不使用
TestingSession
中的实例变量socket
。 Then I noticed that you wrote your entire server "thread" in the constructor, causing start()
to do nothing, and making your server execute only in one single thread. 然后我注意到你在构造函数中编写了整个服务器“thread”,导致
start()
什么都不做,并使你的服务器只在一个线程中执行。
Change this: 改变这个:
class TestingSession extends Thread {
private static final Logger logger = Logger.getLogger(Class.class.getName());
private Socket socket;
TestingSession( Socket socket ) {
try {
DataInputStream dataInputStream = new DataInputStream( socket.getInputStream() );
this.socket = socket;
while( !socket.isClosed() ) {
System.out.println( dataInputStream.readUTF() );
}
} catch( Exception e ) {
}
}
}
To this: 对此:
class TestingSession extends Thread {
private static final Logger logger = Logger.getLogger(Class.class.getName());
private Socket socket;
TestingSession( Socket socket ) {
this.socket = socket;
}
@Override
public void run() {
try {
DataInputStream dataInputStream = new DataInputStream( socket.getInputStream() );
while( !socket.isClosed() ) {
System.out.println( dataInputStream.readUTF() );
}
} catch( Exception e ) {
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.