1.建立ServerSocket,给出一个大于1024的端口
ServerSocket ss = new ServerSocket(10001)
2.调用ServerSocket的accept()方法,等待一个接入的Socket客户端
Socket socket = ss.accept()
3.利用IO流进行通信,建立输入流和输出流
BufferedReader bf = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter pw = new PrintWriter(socket.getOutputStream(), true);
//使用什么流的方式去进行数据交换,根据自己的需求定
4.在此协议上进行通话5.关闭IO流,关闭客户端Socket
pw.close();
bf.close();
socket.close();
6.继续第二步或者第七步
7.关闭ServerSocket
下面是一个多个客户端以多线程的方式与一个服务器通信的例子:
服务器端Java代码如下:
package com.fox.socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
/**
* 服务器
* @author Administrator
*
*/
public class Server extends Thread {
private Socket socket;
private BufferedReader bf;
private PrintWriter pw;
public Server(Socket socket) {
try {
this.socket = socket;
bf = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
pw = new PrintWriter(socket.getOutputStream(), true);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
//监听客户端是否输入,如输入bye,则关闭该客户端的Socket
String line = bf.readLine();
while (!line.equals("bye")) {
System.out.println("输入的是" + line);
line = bf.readLine();
}
pw.println("byebye");
System.out.println(socket+"断开连接");
pw.close();
bf.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new CreateSocket();
}
}
/**
* 创建客户端连接
* 1.建立ServerSocket,给出一个大于1024的端口
* 2.调用ServerSocket的accept()方法,等待一个接入的Socket客户端
* 3.利用IO流进行通信,建立输入流和输出流
* 4.在此协议上进行通话
* 5.关闭IO流,关闭客户端Socket
* 6.继续第二步或者第七步
* 7.关闭ServerSocket
* @author Administrator
*
*/
class CreateSocket {
private ServerSocket ss;
private Socket socket;
public CreateSocket() {
while (true) {
try {
//
ss = new ServerSocket(10001);
while (true) {
socket = ss.accept();
System.out.println(socket);
new Server(socket).start();//为每个Socket客户端开启一个线程
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
客户端Java代码如下:
package com.fox.socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client extends Thread{
private Socket socket;
private BufferedReader bf ;
private PrintWriter pw ;
private Client(){
try {
//创建socket客户端.连接服务器
socket = new Socket("127.0.0.1",10001);
bf = new BufferedReader(new InputStreamReader(socket.getInputStream()));
pw = new PrintWriter(socket.getOutputStream(),true);
start();//开启线程监听用户输入
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
//接收用户输入
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String line = reader.readLine();
while (line!=null) {
System.out.println(line);
pw.println(line); //传值到server
line = reader.readLine();
}
reader.close();
pw.close();
bf.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Client();
}
}