server can also play with the android client apk
server: ChatServer.java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class ChatServer extends Thread {
private ServerSocket socServer = null;
private Socket socClient = null ;
private BufferedReader inClient;
private BufferedWriter outClient;
private int port = 5678;
public ChatServer(int port) throws IOException {
this.port = port;
}
public static void main(String[] arg) throws IOException {
ChatServer chatServer = new ChatServer(5678);
chatServer.watiForClient();
chatServer.start();
}
public void print(Object o) {
System.out.println(o);
}
public void watiForClient() throws IOException {
socServer = new ServerSocket(port);
print("I am Server, ip is " + socServer.getInetAddress() + ", port is " + socServer.getLocalPort());
print("waiting client to connect vv..." );
socClient = socServer.accept();
print("client connectd:" + socClient.getInetAddress());
inClient = new BufferedReader(new InputStreamReader(socClient.getInputStream()));
outClient = new BufferedWriter(new OutputStreamWriter(socClient.getOutputStream()));
}
public void resetSoc() {
try {
if(socClient != null) {
socClient.close();
}
if(socServer != null) {
socServer.close();
}
if(inClient != null)
inClient.close();
if(outClient != null)
outClient.close();
}catch(Exception e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
while(true) {
// print("wait msg from client ..." );
String line = inClient.readLine();
print("receive msg from client : " + line);
if(line == null) {//the peer socket is close
print("the client socket is closed\n\n");
resetSoc();
watiForClient();
}else
{
String sendtxt = "ack\n";
outClient.write(sendtxt);
//outClient.newLine();
print("server send ack");
outClient.flush();
if(line != null && line.equals("quit")) {
print("client request to quit");
break;
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
resetSoc();
}
}
}
client:ChatClient.java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.concurrent.TimeUnit;
public class ChatClient extends Thread {
private Socket socClient = null ;
private BufferedReader inClient;
private BufferedWriter outClient;
private InetAddress serverIP = null;
private int port = 5678;
public ChatClient(InetAddress ip) throws IOException {
serverIP = ip;
}
public static void main(String[] arg) throws IOException {
InetAddress ip = InetAddress.getByName("172.20.10.3");
ChatClient chatClient = new ChatClient(ip);
chatClient.startToConnect();
//testInetAddress();
}
static public void testInetAddress() {
try {
byte[] addr_b = {(byte) 172, (byte) 168, 0, 4}; //is ok
//test string
String host = " 172.160.0.4:5678";
String[] a_s = host.split(":");
InetAddress IP = InetAddress.getByAddress(addr_b); //it is ok
InetAddress IP_by_host = InetAddress.getByName(a_s[0].trim());
System.out.println(IP_by_host);
System.out.println("port " + Integer.parseInt(a_s[1]));
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void startToConnect() throws IOException {
print("server ip : " + serverIP);
connectToServer(serverIP,port);
start();
}
public void connectToServer(InetAddress server,int port) throws IOException {
serverIP = server;
this.port = port;
socClient = new Socket(serverIP, port);
print("connected to "+ server + ":"+port);
inClient = new BufferedReader(new InputStreamReader(socClient.getInputStream()));
outClient = new BufferedWriter(new OutputStreamWriter(socClient.getOutputStream()));
}
static public void print(Object o) {
System.out.println(o);
}
@Override
public void run() {
String msg_test = "Client: I am msg from client ";
int count = 0;
try {
while(true) {
// print("want to send msg to server");
outClient.write("Client : "+ msg_test + count);
outClient.newLine();
outClient.flush();
// String msg = msg_test + ",index " +count + "\n";
// outClientStream.write(msg.getBytes());
// outClientStream.flush();
// print("sent over");
TimeUnit.SECONDS.sleep(2);
String line = inClient.readLine();
print("receive msg from client" + line);
if(line.equals("quit")) {
print("client request to quit");
break;
}
count++;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(socClient != null) {
socClient.close();
}
if(inClient != null)
inClient.close();
if(outClient != null)
outClient.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}
}