用socaket编写客户端与服务端程序相互发送消息

//运行环境:eclipse || MyEclipse

package socaket;

//这些类全放在socaket的包下

//这是服务器端消息发送类

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class ServerChatSend extends Thread {
//服务端发送线程
 Socket ssk = null;
//构造函数
 ServerChatSend(Socket sk) {
  this.ssk = sk;
 }

 public void serverChatSend() {
  try {
//   Socket sk = ssk.accept();
   PrintWriter pw = new PrintWriter(ssk.getOutputStream());
   Scanner scan = new Scanner(System.in);
   String meg = null;
   while((meg = scan.nextLine())!= null) {
    pw.println(meg);
    pw.flush();
   }
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 @Override
 public void run() {
  serverChatSend();
 }

}

---------------------------------------------------------------

package socaket;

//服务端消息接收

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

//服务器端接收线程
public class ServerChatReceive extends Thread {
 private Socket sk = null;
//构造器
 public ServerChatReceive(Socket sk) {
  this.sk = sk;
 }

 public void serverChatReceive() {
  try {
   BufferedReader bfr = new BufferedReader(new InputStreamReader(
     sk.getInputStream()));
   String meg =null;
   while ((meg = bfr.readLine()) != null) {
    System.out.println("Client: " + meg);
   }
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 
 public void serverFileReceive(){
  
 }
@Override
 public void run() {
  serverChatReceive();
 }
}

--------------------------------------------------------

//客户端消息发送

package socaket;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

//客户端发送线程
public class ClientChatSend extends Thread {
 private Socket sk=null;
// 构造函数
 ClientChatSend(Socket sk){
  this.sk=sk;
 }

 public void clientChatSend() {
  try {
   Scanner scan = new Scanner(System.in);
   PrintWriter pw = new PrintWriter(sk.getOutputStream());
   String meg = null;
   while(true){
   if((meg =scan.nextLine())!= null) {
    pw.println(meg);
    pw.flush();
   }
   }
  } catch (UnknownHostException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }

 }
 @Override
 public void run() {
  clientChatSend();
 }

}

--------------------------------------------------------

//客户端消息接收

package socaket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;

//客户端接收线程
public class ClientChatReceive extends Thread{
 private Socket sk=null;
 
 ClientChatReceive(Socket sk){
  this.sk=sk;
 }
// 消息的接收
 public void clientChatReceive(){
  try {
   
   BufferedReader bfr=new BufferedReader(new InputStreamReader(sk.getInputStream()));
   String meg=null;
   while((meg=bfr.readLine())!=null){
    System.out.println("Server: "+meg);
   }
  } catch (UnknownHostException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 @Override
 public void run() {
  clientChatReceive();
 }
}

-------------------------------------------------------

//服务端主方法

package socaket;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerChat {
// 服务端主程序
 public static void main(String[] args) {
  try {
//   建立连接
   ServerSocket ssk = new ServerSocket(1234);
   Socket sk = ssk.accept();
//   创建对象
   ServerChatReceive scr = new ServerChatReceive(sk);
   ServerChatSend scs = new ServerChatSend(sk);
//   启动线程
   scr.start();
   scs.start();
   System.out.println("服务器端程序已启动!");

  } catch (IOException e) {
   e.printStackTrace();
  }
 }

}

-----------------------------------------------------------

//客户端主方法

package socaket;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

public class ClientChat {
// 客户端主程序
 public static void main(String[] args) {
  try {
//   本机的连接测试
   Socket sk = new Socket("127.0.0.1", 1234);
   ClientChatReceive ccr=new ClientChatReceive(sk);
   ClientChatSend ccs=new ClientChatSend(sk);
//   线程的启动
   ccr.start();
   ccs.start();
   System.out.println("客户端程序已经启动!");
  } catch (UnknownHostException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  
 }

}

-------------------------------------

备注:大家在运行时先运行服务端主方法,再运行客户端主方法就可以相互发送消息了,这里的程序是在本机上测试,如果要和别人测试的话,一个运行客户端,一个运行服务端,改好相应的IP和端口号就可以了>_<注意哦,不管怎样测试,先开服务器端就对了

posted @ 2015-07-20 13:36  疯狂麦洛  阅读(341)  评论(0编辑  收藏  举报