这是前几天写的一个局域网两台机器相互聊天的功能的代码 你改一下应该就可以了
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class MyClient {
public static void main(String[] args ) throws UnknownHostException, IOException{
Socket s=new Socket("172.28.138.185",30000);
new ClientIn(s).start();
new ClientOut(s).start();
}
}
class ClientIn extends Thread{
Socket s;
BufferedReader br;
public ClientIn(Socket s) throws IOException{
this.s=s;
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
}
public void run(){
try {
String str;
while((str=br.readLine())!=null){
System.out.println(str);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class ClientOut extends Thread{
private Socket s=null;
BufferedReader br;
BufferedWriter bw;
BufferedReader br1;
public ClientOut(Socket s) throws IOException{
this.s=s;
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
br1=new BufferedReader(new InputStreamReader(System.in));
}
public void run(){
try {
String str;
while((str=br1.readLine())!=null){
bw.write(str);
bw.newLine();
bw.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
br1.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class MyServer {
public static ArrayList<Socket> list=new ArrayList();
public static void main(String[] args) throws IOException{
ServerSocket ss=new ServerSocket(30000);
while(true){
Socket s=ss.accept();
list.add(s);
new ServerOut(s).start();
new ServerIn(s).start();
}
}
}
class ServerOut extends Thread{
private Socket s=null;
BufferedReader br;
BufferedWriter bw;
BufferedReader br1;
public ServerOut(Socket s) throws IOException{
this.s=s;
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
br1=new BufferedReader(new InputStreamReader(System.in));
}
public void run(){
InetAddress ip=s.getInetAddress();
System.out.println(ip+"已连接.....");
try {
bw.write("您已成功连接服务器");
bw.newLine();
bw.flush();
String str;
while((str=br1.readLine())!=null){
bw.write(str);
bw.newLine();
bw.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
br1.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class ServerIn extends Thread{
Socket s;
BufferedReader br;
public ServerIn(Socket s) throws IOException{
this.s=s;
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
}
public void run(){
try {
String str;
while((str=br.readLine())!=null){
System.out.println(str);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
|