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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

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端

3 个回复

倒序浏览
找到错误了,因为你少加了一个循环控制
import java.net.*;
import java.io.*;
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){                        //就是这里了,要加个死循环
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);
}
}

}

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);
}
}
}
可以连多个客户端,但是原来的程序其实还有一些其他问题
运行结果

socket.png (29.78 KB, 下载次数: 22)

socket.png
回复 使用道具 举报
我讲楼主的代码改了改,我这边运行通过了,也可以连接多个客户端了
服务端:
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) {
                        s = ss.accept(); // 阻塞式的方法,此方法在连接传入之前一直阻塞
                        // 每链接一个客户端就开启一个新的线程为期服务
                        new Thread(new ClientThread(s)).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 data = dis.readUTF();
                                System.out.println("客户端数据:" + data);
                        } catch (IOException e) {
                                System.out.println("客户端已经断开连接!");
                                try {
                                        s.close();
                                        dis.close();
                                } catch (IOException e1) {
                                        e1.printStackTrace();
                                }
                                return;// 如果客户端断开了链接,要结束这个线程
                        }
                }
        }
}


客户端:
import java.net.*;
import java.io.*;

class DOSClient {
        public static void main(String[] args) throws Exception {
                Socket s = new Socket("127.0.0.1", 8880);
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                DataOutputStream dos = new DataOutputStream(s.getOutputStream());
                String data = "";
                //循环想服务端输出数据
                while (true) {
                        System.out.println("请输入:");
                        data = br.readLine();
                        //如果输入exit,结束当前客户端
                        if (data.equals("exit")) {
                                s.shutdownOutput();
                                br.close();
                                dos.close();
                                return;
                        }
                        dos.writeUTF(data);
                }

        }
}


回复 使用道具 举报
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();     reanLine()是阻塞是方法,没有读到数据,一直等待。
                                                    因为在while循环里,所以不会再执行了,也就不会接受另外一个client了

//dos.writeUTF(str2);
}
}

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马