SocketClient1.java内容如下:
package com.polyphylla.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 SocketClient1 {
private Socket socket;
public SocketClient1() {
try {
socket = new Socket("127.0.0.1", 2222);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
new SocketClient1().startClient();
}
public void startClient() {
new Thread(new ClientReader(socket)).start();
new Thread(new ClientWriter(socket)).start();
}
}
class ClientReader implements Runnable {
private Socket socket;
public ClientReader(Socket socket) {
this.socket = socket;
}
public void run() {
BufferedReader br = getReader();
String msg = null;
try {
while ((msg = br.readLine()) != null) {
System.out.println("client:" + msg);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (socket != null)
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private BufferedReader getReader() {
try {
return new BufferedReader(new InputStreamReader(socket
.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
class ClientWriter implements Runnable {
private Socket socket;
public ClientWriter(Socket socket) {
this.socket = socket;
}
public void run() {
PrintWriter pw = getWriter();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = null;
try {
while ((str = br.readLine()) != null) {
pw.println(str);
pw.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (pw != null)
pw.close();
try {
if (socket != null)
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private PrintWriter getWriter() {
try {
return new PrintWriter(socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
SocketServer1.java内容如下:
package com.polyphylla.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;
public class SocketServer1 {
private ServerSocket ss;
public SocketServer1() {
try {
ss = new ServerSocket(2222);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new SocketServer1().startServer();
}
public void startServer() {
try {
while (true) {
Socket s = ss.accept();
new Thread(new ServerReader(s)).start();
new Thread(new ServerWriter(s)).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class ServerReader implements Runnable {
private Socket socket;
public ServerReader(Socket socket) {
this.socket = socket;
}
public void run() {
BufferedReader br = getReader();
String msg = null;
try {
while((msg = br.readLine()) != null) {
System.out.println("server:" + msg);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(br != null)
br.close();
if(socket != null)
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private BufferedReader getReader() {
try {
return new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
class ServerWriter implements Runnable {
private Socket socket;
public ServerWriter(Socket socket) {
this.socket = socket;
}
public void run() {
PrintWriter pw = getWriter();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = null;
try {
while((str = br.readLine()) != null) {
pw.println(str);
pw.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (pw != null)
pw.close();
try {
if (socket != null)
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private PrintWriter getWriter() {
try {
return new PrintWriter(socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}