chat-server-client(pc app in java)

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();
			}
			
		}
	}
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值