接收端
package Tcp;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
//接收端
public class a implements Runnable {
Socket s;
public a(Socket s){
this.s=s;
}
public void run() {
byte[] bys=new byte[1024];
String line;
boolean a=true;
BufferedReader br=null;BufferedWriter bw=null;BufferedReader br1=null;
try {
br= new BufferedReader(new InputStreamReader(this.s.getInputStream()));
bw = new BufferedWriter(new OutputStreamWriter(this.s.getOutputStream()));
br1= new BufferedReader(new InputStreamReader(System.in));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
if("1".equals(Thread.currentThread().getName())){
while(a){
line=br.readLine();
if(line==null){
break;
}
System.out.println(line);
}
}else{
while(a){
line=br1.readLine();
if(line==null||line==" "){
break;
}
bw.write("wu :"+line);
bw.newLine();
bw.flush();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(br1!=null)
br1.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(10086);
Socket s = ss.accept();
a a=new a(s);
Thread thread = new Thread(a,"1");
thread.start();
Thread thread1 = new Thread(a,"2");
thread1.start();
System.out.println();
}
}
发送端
package TCP;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
//tcp发送端
public class ClientDemo implements Runnable {
static Socket s ;
BufferedWriter bw ;
BufferedReader br1;
BufferedReader br ;
public ClientDemo(Socket s) {
try {
this.s = s;
bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
br1 = new BufferedReader(new InputStreamReader(s.getInputStream()));
br= new BufferedReader(new InputStreamReader(System.in));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run() {
String line;
boolean a=true;
try {
if("1".equals(Thread.currentThread().getName())){
while(a){
line=this.br.readLine();
if(line==null||line==" "){
break;
}
this.bw.write("li :"+line);
this.bw.newLine();
this.bw.flush();
}
}else{
while(a){
line=this.br1.readLine();
if(line==null){
break;
}
System.out.println(line);
}
}
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(this.br!=null)
this.br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(this.s!=null)
this.s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) throws Exception {
Socket s = new Socket("192.168.1.132", 10086);
ClientDemo clientDemo = new ClientDemo(s);
Thread thread = new Thread(clientDemo,"1");
thread.start();
Thread thread1 = new Thread(clientDemo,"2");
thread1.start();
}
}