Server.java
package socket; import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket; public class Server {
//监听的端口
public static final int PORT = 54039; public static void main(String[] args)
{
new Server().start();
} //启动监听线程的方法
public void start()
{
ServerSocket serverSocket = null;
try {
//新建一个ServerSocket对象,监听PORT端口,可能会抛出IOException
serverSocket = new ServerSocket(PORT);
System.out.println("serverScoket:" + serverSocket);
//循环监听
while(true)
{
//获取客户端连接,如果没有则阻塞
Socket clientSocket = serverSocket.accept();
//启动线程处理监听到的数据并启动线程
new Thread(new DataRunnable(clientSocket)).start(); }
} catch (IOException e) {
e.printStackTrace();
} finally{
if (null != serverSocket)
{
try {
serverSocket.close();
System.out.println("close serverSocket success!");
} catch (IOException e) {
System.out.println("close serverSocket failed!");
}
}
} } /**
* 处理监听信息的job
*/
private class DataRunnable implements Runnable
{
Socket client; public DataRunnable(Socket client) {
super();
this.client = client;
} public void run() {
try {
//读取客户端socket取输入流,包装成DataInputStream
DataInputStream dataIn = new DataInputStream(client.getInputStream());
DataOutputStream dataOut = new DataOutputStream(client.getOutputStream());
String content = dataIn.readUTF();//有read其他格式的方法,具体见api
//打印到控制台
System.out.println("content read in from client:" + content); //向客户端回复信息
System.out.println("请输入要回复的内容:");
String reply = new BufferedReader(new InputStreamReader(System.in)).readLine();
dataOut.writeUTF(reply); if("Close".equals(content))
{
System.out.println("client has been closed ,i want to be closed too");
}
dataIn.close();
dataOut.close();
} catch (IOException e) {
e.printStackTrace();
} } }
}
Client.java
package socket; import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException; public class Client {
public static final String ADDR = "localhost";//监听本机
public static final int PORT = 54039;//端口 public static void main(String[] args)
{
while(true)
{
Socket client = null;
DataInputStream dataIn = null;
DataOutputStream dataout = null;
try {
//创建客户端socket连接到主机服务器端
client = new Socket(ADDR, PORT); dataIn = new DataInputStream(client.getInputStream());
dataout = new DataOutputStream(client.getOutputStream()); //向服务器发送数据
System.out.println("请输入数据:");
String content = new BufferedReader(new InputStreamReader(System.in)).readLine();
dataout.writeUTF(content); //读取服务器端返回的数据
String reply = dataIn.readUTF();
System.out.println("server reply:" + reply); } catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
if(null!=dataIn) dataIn.close();
if(null!=dataout) dataout.close();
if(null!=client) client.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}