博客:http://blog.csdn.net/muyang_ren
发送与接收线程
Recv.java
package lhy.socket.server;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
public class Recv implements Runnable{
String str = null;
private Socket client = null;
private InputStreamReader recv = null;
private BufferedReader reader = null;
public Recv(Socket c) {
this.client = c;
}
public void run() {
System.out.println("recv thread");
//System.out.flush();
try {
recv = new InputStreamReader(client.getInputStream());
reader = new BufferedReader(recv);
while (!client.isClosed()) {
str = reader.readLine();
System.out.println(str);
}
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Send.java
package lhy.socket.server;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class Send implements Runnable{
private Socket client = null;
private static OutputStreamWriter send = null;
private static PrintWriter writer = null;
Scanner sin = null;
String msgstr = null;
String sendstr = null;
//c :客户端或者服务器套接字
//str : 存放客户端或者服务器发送过来的字符串:Server 或者 Client
public Send(Socket c,String str) {
this.client = c;
this.sendstr = str;
}
public void run() {
System.out.println("send thread");
sin = new Scanner(System.in);
try {
send = new OutputStreamWriter(client.getOutputStream());
writer = new PrintWriter(send,true);
while (!client.isClosed()) {
msgstr = sin.nextLine();
writer.println(sendstr+client.getInetAddress()+":"+msgstr);
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
服务器
TestServer.java
package lhy.socket.server;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class TestServer {
private static ServerSocket server;
private static final int TCP_PORT = 3002;
/**
* @param args
*/
public static void main(String[] args)
{
System.out.println("server->致:hello world! 最伟大的程序!");
try {
//1.设置服务器
server = new ServerSocket(TCP_PORT);
System.out.println("服务器已经启动!");
while (true) {
//2.监听客户端,这里只实现了与一个客户端的通信
//【如果要和多个客户端进行通信,将连接的客户端通过监
//听将客户端保存到集合内,发送只开一个线程就够了,重
//写我send方法,比如实现发送时历遍集合发送;而服务器
//接收信息时就需要为每一次监听到的客户端开一个线程】
Socket client =server.accept();
//3.开接收线程
Recv readthread = new Recv(client);
new Thread(readthread,"Server-Read-Thread").start();
Send sendthread = new Send(client,"Server");
new Thread(sendthread,"Server-Send-Thread").start();
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
客户端
package lhy.socket.server;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class TestClient {
private static Socket clienSocket;
private final static String TCP_IP ="127.0.0.1";
private final static int TCP_PORT = 3002;
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("client->致:hello world! 最伟大的程序!");
try {
clienSocket = new Socket(TCP_IP, TCP_PORT);
Recv readthread = new Recv(clienSocket);
new Thread(readthread,"接收").start();
Send sendthread = new Send(clienSocket,"Client");
new Thread(sendthread,"发送").start();
} catch (UnknownHostException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
原文地址:http://blog.csdn.net/muyang_ren/article/details/46493007