黑马程序员技术交流社区
标题: 学到网络编程,有问题,求解释 ? [打印本页]
作者: 创出一片辉煌 时间: 2012-7-27 09:47
标题: 学到网络编程,有问题,求解释 ?
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端
作者: 胡文凡 时间: 2012-7-27 10:37
找到错误了,因为你少加了一个循环控制
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, 下载次数: 21)
作者: 王志明 时间: 2012-7-27 12:27
我讲楼主的代码改了改,我这边运行通过了,也可以连接多个客户端了
服务端:
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);
}
}
}
作者: 李菁 时间: 2012-7-27 12:27
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);
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) |
黑马程序员IT技术论坛 X3.2 |