参考:
郭霖 见证Android消息推送时刻
Client
package com.imooc.client;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
public class SocketClient {
public static void main(String[] args) {
SocketClient client = new SocketClient();
client.start();
}
public void start() {
BufferedReader inputReader = null;
BufferedReader reader = null;
BufferedWriter writer = null;
Socket socket = null;
try {
socket = new Socket("127.0.0.1", 9898);
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
inputReader = new BufferedReader(new InputStreamReader(System.in));
startServerReplyListener(reader);
String inputContent;
int count = 0;
while (!(inputContent = inputReader.readLine()).equals("bye")) {
writer.write(inputContent);
if (count % 2 ==0) {
writer.write("\n");
}
count++;
writer.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
reader.close();
writer.close();
inputReader.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void startServerReplyListener(final BufferedReader reader) {
new Thread(new Runnable() {
@Override
public void run() {
try {
String response;
while ((response = reader.readLine()) != null) {
System.out.println(response);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}
Server
package com.imooc.server;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class SocketServer {
public static void main(String[] args) {
SocketServer socketServer = new SocketServer();
socketServer.startServer();
}
public void startServer() {
ServerSocket serverSocket = null;
Socket socket = null;
try {
serverSocket = new ServerSocket(9898);
System.out.println("server started..");
while (true) {
socket = serverSocket.accept();
manageConnection(socket);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
socket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void manageConnection(final Socket socket) {
new Thread(new Runnable() {
@Override
public void run() {
BufferedReader reader = null;
BufferedWriter writer =null;
try {
System.out.println("client " + socket.hashCode() + " connedted");
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String receivedMsg;
while ((receivedMsg = reader.readLine()) != null) {
System.out.println("client " + socket.hashCode() + ": " + receivedMsg);
writer.write("server reply: " + receivedMsg + "\n");
writer.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}).start();
}
}