A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 陈红建 中级黑马   /  2012-8-3 07:34  /  1446 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

Server端:import java.net.*;
import java.io.*;
public class DOSServer {
private static Socket s = null;
private static ServerSocket ss = null;
public static void main (String[] args) throws Exception {
ss = new ServerSocket (8880);
s = ss.accept(); //阻塞式的方法,此方法在连接传入之前一直阻塞
ClientThread ct = new ClientThread(s);
new Thread(ct).start();
System.out.println ("客户端已经连接!");
}
}
class ClientThread implements Runnable {  
private Socket s;
private DataInputStream dis;
public ClientThread (Socket s) throws Exception {
this.s = s;
dis = new DataInputStream(s.getInputStream());
}
public void run() {
while (true) {
try {
String str1;
str1 = dis.readUTF();
System.out.println(str1);
} catch (IOException e) {
e.printStackTrace();
}
//DataOutputStream dos = new DataOutputStream(s.getOutputStream());
//BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//String str2 = "Server: " + br.readLine();
//dos.writeUTF(str2);
}
}
}
Client端:
import java.net.*;
import java.io.*;
public class DOSClient {
public static void main (String[] args) throws Exception {
Socket s = new Socket("127.0.0.1", 8880);
while (true) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str1 = "Client: " + br.readLine();
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(str1);
//DataInputStream dis = new DataInputStream(s.getInputStream());
//String str2 = dis.readUTF();
//System.out.println(str2);
}
}
}

提问:为什么Server端只能连接一个Client端

4 个回复

倒序浏览
import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class DOSServer {
private static Socket s = null;
private static ServerSocket ss = null;
public static void main (String[] args) throws Exception {
ss = new ServerSocket (8880);
while(true) 这里加个while(true)就好了,让server端不断等待客户端,否则server端接收一个客户端后,便不再做接收client端的事情了。
{
s = ss.accept(); //阻塞式的方法,此方法在连接传入之前一直阻塞
ClientThread ct = new ClientThread(s);
new Thread(ct).start();
System.out.println ("客户端已经连接!");
}
}
}
class ClientThread implements Runnable {  
        private Socket s;
        private DataInputStream dis;
        public ClientThread (Socket s) throws Exception
        {
        this.s = s;
        dis = new DataInputStream(s.getInputStream());
    }
    public void run() {
                while (true) {
                try {
                String str1;
                str1 = dis.readUTF();
                System.out.println(str1);
                } catch (IOException e) {
                e.printStackTrace();
                }
//DataOutputStream dos = new DataOutputStream(s.getOutputStream());
//BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//String str2 = "Server: " + br.readLine();
//dos.writeUTF(str2);
    }
}
}
回复 使用道具 举报
标题回复求加分啊。解决了
回复 使用道具 举报
标题求加分啊!解决啦!手机党伤不起啊!
回复 使用道具 举报
标题求加分啊!解决啦!手机党伤不起啊!二楼回复求加分啊!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马