客户端
package cn.qianfeng.kaoti03; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket; class ReceiveSocket implements Runnable {
private Socket s; public ReceiveSocket(Socket s) {
super();
this.s = s;
} @Override
public void run() {
// TODO Auto-generated method stub
BufferedReader br = null;
try {
while (true) { br = new BufferedReader(new InputStreamReader(
s.getInputStream()));
String line = br.readLine();
if (line != null) {
System.out.println(line);
} }
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} }
} class SendSocket implements Runnable {
private Socket s; public SendSocket(Socket s) {
super();
this.s = s;
} @Override
public void run() {
// TODO Auto-generated method stub
BufferedReader br = null;
try {
while (true) { br = new BufferedReader(new InputStreamReader(System.in));
PrintStream ps = new PrintStream(s.getOutputStream(), true);
String line = br.readLine();
ps.println("A:" + line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} } }
} public class TestSocket {
public static void main(String[] args) {
try {
Socket s = new Socket("localhost", 8888);
new Thread(new SendSocket(s)).start();
new Thread(new ReceiveSocket(s)).start(); } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
服务端
package cn.qianfeng.kaoti03; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket; class SendServerSocket implements Runnable {
private Socket s; public SendServerSocket(Socket s) {
super();
this.s = s;
} @Override
public void run() {
// TODO Auto-generated method stub
BufferedReader br = null;
PrintWriter ps = null;
try {
while (true) { br = new BufferedReader(new InputStreamReader(System.in));
ps = new PrintWriter(s.getOutputStream(), true);
String line = br.readLine();
if (line != null) {
ps.println("B:" + line);
} }
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} } }
} class ReceiveServerSocket implements Runnable {
private Socket s; public ReceiveServerSocket(Socket s) {
super();
this.s = s;
} @Override
public void run() {
// TODO Auto-generated method stub
BufferedReader br = null;
try {
while (true) { br = new BufferedReader(new InputStreamReader(
s.getInputStream())); String line = br.readLine();
if (line != null) {
System.out.println(line); }
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} } } } public class TestServerSocket {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(8888);
Socket s = ss.accept();
System.out.println(s.getInetAddress().getHostName()
+ ".....connected"); new Thread(new SendServerSocket(s)).start();
new Thread(new ReceiveServerSocket(s)).start(); } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}