这是Client端
import java.io.*;
import java.net.*;public class TalkClient {
public static void main(String[] args) throws Exception {
InputStream is = null;
OutputStream os = null;
Socket s = null;
while(true) {
try {
s = new Socket("127.0.0.1",6688);
} catch (UnknownHostException e) {
System.out.println("无法连接服务器");
}
try{
os = s.getOutputStream();
is = s.getInputStream();
DataOutputStream dos = new DataOutputStream(os);
DataInputStream dis = new DataInputStream(is);
System.out.print("Client: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
dos.writeUTF("Client: "+line);
dos.flush();
System.out.println(dis.readUTF());
dos.close();
dis.close();
s.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}
}
这是Server端import java.io.*;
import java.net.*;public class TalkServer {
public static void main(String[] args) throws Exception {
InputStream is = null;
OutputStream os = null;
ServerSocket ss = null;
try {
ss = new ServerSocket(6688);
} catch(UnknownHostException e) {}
try{
while(true) {
Socket s = ss.accept();
is = s.getInputStream();
os = s.getOutputStream();
DataInputStream dis = new DataInputStream(is);
DataOutputStream dos = new DataOutputStream(os);
System.out.println(dis.readUTF());
System.out.print("Server: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
dos.writeUTF("Server: "+line);
dos.flush();
dis.close();
dos.close();
s.close();
}
} catch(IOException e) {
e.printStackTrace();
}
}
}
import java.io.*;
import java.net.*;public class TalkClient {
public static void main(String[] args) throws Exception {
InputStream is = null;
OutputStream os = null;
Socket s = null;
while(true) {
try {
s = new Socket("127.0.0.1",6688);
} catch (UnknownHostException e) {
System.out.println("无法连接服务器");
}
try{
os = s.getOutputStream();
is = s.getInputStream();
DataOutputStream dos = new DataOutputStream(os);
DataInputStream dis = new DataInputStream(is);
System.out.print("Client: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
dos.writeUTF("Client: "+line);
dos.flush();
System.out.println(dis.readUTF());
dos.close();
dis.close();
s.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}
}
这是Server端import java.io.*;
import java.net.*;public class TalkServer {
public static void main(String[] args) throws Exception {
InputStream is = null;
OutputStream os = null;
ServerSocket ss = null;
try {
ss = new ServerSocket(6688);
} catch(UnknownHostException e) {}
try{
while(true) {
Socket s = ss.accept();
is = s.getInputStream();
os = s.getOutputStream();
DataInputStream dis = new DataInputStream(is);
DataOutputStream dos = new DataOutputStream(os);
System.out.println(dis.readUTF());
System.out.print("Server: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
dos.writeUTF("Server: "+line);
dos.flush();
dis.close();
dos.close();
s.close();
}
} catch(IOException e) {
e.printStackTrace();
}
}
}
import java.net.*;public class TalkServer {
public static void main(String[] args) {
InputStream is = null;
OutputStream os = null;
ServerSocket ss = null;
try {
ss = new ServerSocket(6688);
} catch(UnknownHostException e) {
System.out.println("无法监听端口");
} catch(IOException e) {
e.printStackTrace();
}
try{
while(true) {
Socket s = ss.accept();
is = s.getInputStream();
os = s.getOutputStream();
DataInputStream dis = new DataInputStream(is);
DataOutputStream dos = new DataOutputStream(os);
System.out.println(dis.readUTF());
System.out.print("Server: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
dos.writeUTF("Server: "+line);
dos.flush();
dis.close();
dos.close();
s.close();
}
} catch(IOException e) {
e.printStackTrace();
}
}
}
Client 端import java.io.*;
import java.net.*;public class TalkClient {
public static void main(String[] args) {
InputStream is = null;
OutputStream os = null;
Socket s = null;
while(true) {
try {
s = new Socket("127.0.0.1",6688);
} catch (UnknownHostException e) {
System.out.println("无法连接服务器");
} catch(IOException e) {
e.printStackTrace();
}
try{
os = s.getOutputStream();
is = s.getInputStream();
DataOutputStream dos = new DataOutputStream(os);
DataInputStream dis = new DataInputStream(is);
System.out.print("Client: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
dos.writeUTF("Client: "+line);
dos.flush();
System.out.println(dis.readUTF());
dos.close();
dis.close();
s.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}
}添加,例如,无论是客户端还是服务端,输入QUIT就可以结束循环,退出程序
import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;public class TalkServer implements Runnable { /**
* 服务端 Socket
*/
private ServerSocket serverSocket; /**
* 监听端口号
*/
private int port; /**
* 服务是否工作
*/
private volatile boolean open; /**
* 处理客户端数据的线程池
*/
private ExecutorService executor; public TalkServer(int port, int max) throws IOException{
this.port = port;
this.open = true;
this.serverSocket = new ServerSocket(this.port);
this.executor = Executors.newFixedThreadPool(max);
} /**
* 引导类
*
* @param args
* @throws IOException
*
* @author frankiegao123
* 2010-11-2下午10:47:27
*/
public static void main(String[] args) throws IOException {
TalkServer server = new TalkServer(9000, 5);
new Thread(server).start();
server.service();
} /**
* 主线程:用于处理客户端数据
*
* @author frankiegao123
* 2010-11-2下午10:45:29
*/
public void service() {
while(open) {
try {
Socket socket = serverSocket.accept();
executor.execute(new Handler(socket)); // 使用线程池中的线程处理客户端的数据
} catch (IOException e) {
if(!e.getMessage().equals("socket closed")) {
e.printStackTrace();
}
}
}
} /**
* 用于接收用户控制台的命令输入
*/
public void run() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String prompt = "[Server: " + port + "]$ ";
while(open) {
try {
System.out.print(prompt);
String command = br.readLine();
if("quit".equalsIgnoreCase(command)) {
shutdown();
} else {
System.out.println("command illegal, please input QUIT");
}
} catch (IOException e) {
e.printStackTrace();
}
}
} private void shutdown() {
open = false;
executor.shutdown();
try {
serverSocket.close();
} catch (IOException e) {
// Ignore
} finally {
System.out.println("server has been shutdown");
}
} /**
* 接收到客户端数据时的处理
*
* @author frankiegao123
* 2010-11-2 下午10:45:51
*/
private static class Handler implements Runnable { private Socket socket; public Handler(Socket socket) {
this.socket = socket;
} public void run() {
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "GBK"));
bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "GBK"));
String str = br.readLine();
bw.write("ECHO: " + str);
bw.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
closeIO(br);
closeIO(bw);
close();
}
} private void close() {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} private static void closeIO(Closeable io) {
if(io != null) {
try {
io.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}