多个线程能监听一个端口吗? 如果这个端口发送数据,多个线程能同时接收吗? 代码该怎么写。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Server端:package server;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;import java.util.ArrayList;public class ServerText { ServerSocket serversocket=null; Socket socket=null; ArrayList<Client> clients=new ArrayList<Client>(); boolean con=false; public void start() { try { serversocket=new ServerSocket(8888);//设置服务器端口号 } catch (IOException e) { System.out.println("服务器启动失败");//可能是当前的端口号已被占用 } System.out.println("服务器启动成功"); while(con==true) { try { socket=serversocket.accept(); Client client=new Client(); Thread t=new Thread(client); t.start(); clients.add(client); } catch (IOException e) { e.printStackTrace(); } } } class Client implements Runnable { DataInputStream in=null; DataOutputStream out=null; Client() { try { in=new DataInputStream(socket.getInputStream()); out=new DataOutputStream(socket.getOutputStream()); } catch(Exception ae) { ae.printStackTrace(); } } public void write(String str1) { try { out.writeUTF(str1); out.flush(); } catch (IOException e) { e.printStackTrace(); } } public void run() { String str2=""; while(con) { try { str2=in.readUTF(); } catch (IOException e) { System.out.println("客户端异常退出"); try { in.close(); out.close(); } catch (IOException e1) { e1.printStackTrace(); } break; } } } } public static void main(String args[]) { ServerText servertext=new ServerText(); servertext.start(); for(int i=0;i<servertext.clients.size();i++) { servertext.clients.get(i).write("你妈叫你吃饭了"); } } } Client端:package server;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.net.Socket;public class ClientText implements Runnable{ Mythread mythread=null; Socket socket=null; boolean con=false; DataInputStream in=null; DataOutputStream out=null; public void connect() { try { socket=new Socket("127.0.0.1",8888); in=new DataInputStream(socket.getInputStream()); out=new DataOutputStream(socket.getOutputStream()); con=true; } catch (Exception ae) { ae.printStackTrace(); } } public void run() { String str=""; while(con) { try { str=in.readUTF(); } catch (IOException e) { System.out.println("服务器端出错"); try { in.close(); out.close(); } catch (IOException e1) { e1.printStackTrace(); } } System.out.println(str); } } class Mythread implements Runnable { public void run() { try { out.writeUTF("娃哈哈,O(∩_∩)O~"); } catch (IOException e) { System.out.println("服务器端出错"); try { in.close(); out.close(); } catch (IOException e1) { e1.printStackTrace(); } } try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String args[]) { for(int i=1;i<=10;i++) { ClientText clienttext=new ClientText(); clienttext.connect(); Thread t=new Thread(clienttext); t.start(); Thread tt=new Thread(clienttext.mythread); tt.start(); } }} 抛个屁。lz是多个线程共用一个端口。你是一个线程对应一个端口。差多了。lz可以试试,服务器在发送消息的时候,把对应线程的标识也跟着发送过去。收到数据的线程只要判断标识是不是属于自己的就知道。注:实例化一个线程后,通过thread1.getId()获得该线程的标识。 ServerSocket类不能实例化两个相同端口的对象!不然会抛出一个异常Exception in thread "main" java.net.BindException: Address already in use: JVM_Bindimport java.net.ServerSocket;import java.io.IOException;public class ThreadException { public static void main(String[] args) throws IOException { ServerSocket sever1 = new ServerSocket(9090); ServerSocket sever2 = new ServerSocket(9090); }} 字符串的解析 getContentPane()显示不出来 java 连oracle 空指针异常 帮忙看哈 !! 最后30分了。 时间只剩不下多少 请kao331431214进来下 请教下ECLIPSE导出JAVA程序 paint问题 Java3D骨骼动作加贴图.已经开源 请教,java数组如何存储超过int范围数据 用java写的一个简单的播放器运行却爆没有权限操作文件!有遇到过的大神么 请教:使用java.net中httpURLConnection类模拟post方法?、、全部分数送上! 如何定义StringBuilder组 求助,一个闹钟的测试程序,加入的音频文件路径该如何写
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;public class ServerText {
ServerSocket serversocket=null;
Socket socket=null;
ArrayList<Client> clients=new ArrayList<Client>();
boolean con=false;
public void start() {
try {
serversocket=new ServerSocket(8888);//设置服务器端口号
} catch (IOException e) {
System.out.println("服务器启动失败");//可能是当前的端口号已被占用
}
System.out.println("服务器启动成功");
while(con==true) {
try {
socket=serversocket.accept();
Client client=new Client();
Thread t=new Thread(client);
t.start();
clients.add(client);
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Client implements Runnable {
DataInputStream in=null;
DataOutputStream out=null;
Client() {
try {
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
} catch(Exception ae) {
ae.printStackTrace();
}
}
public void write(String str1) {
try {
out.writeUTF(str1);
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
} public void run() {
String str2="";
while(con) {
try {
str2=in.readUTF();
} catch (IOException e) {
System.out.println("客户端异常退出");
try {
in.close();
out.close();
} catch (IOException e1) {
e1.printStackTrace();
}
break;
}
}
}
}
public static void main(String args[]) {
ServerText servertext=new ServerText();
servertext.start();
for(int i=0;i<servertext.clients.size();i++) {
servertext.clients.get(i).write("你妈叫你吃饭了");
}
}
}
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;public class ClientText implements Runnable{
Mythread mythread=null;
Socket socket=null;
boolean con=false;
DataInputStream in=null;
DataOutputStream out=null;
public void connect() {
try {
socket=new Socket("127.0.0.1",8888);
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
con=true;
} catch (Exception ae) {
ae.printStackTrace();
}
}
public void run() {
String str="";
while(con) {
try {
str=in.readUTF();
} catch (IOException e) {
System.out.println("服务器端出错");
try {
in.close();
out.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
System.out.println(str);
}
}
class Mythread implements Runnable {
public void run() {
try {
out.writeUTF("娃哈哈,O(∩_∩)O~");
} catch (IOException e) {
System.out.println("服务器端出错");
try {
in.close();
out.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String args[]) {
for(int i=1;i<=10;i++) {
ClientText clienttext=new ClientText();
clienttext.connect();
Thread t=new Thread(clienttext);
t.start();
Thread tt=new Thread(clienttext.mythread);
tt.start();
}
}
}
lz可以试试,服务器在发送消息的时候,把对应线程的标识也跟着发送过去。收到数据的线程只要判断标识是不是属于自己的就知道。
注:实例化一个线程后,通过thread1.getId()获得该线程的标识。
Exception in thread "main" java.net.BindException: Address already in use: JVM_Bindimport java.net.ServerSocket;
import java.io.IOException;public class ThreadException {
public static void main(String[] args) throws IOException {
ServerSocket sever1 = new ServerSocket(9090); ServerSocket sever2 = new ServerSocket(9090);
}
}