首页 > 代码库 > Java socket模拟聊天程序

Java socket模拟聊天程序



根据TCP/IP协议,通过socket实现网络聊天,分为服务器端和客户端,一下是参考程序。

服务器端程序:

package com.michael.contact;

import java.io.BufferedReader;
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 void main(String args[]) throws IOException {
		String reces = null;
		int len;
		Socket socket = null;
		OutputStream outputstream = null;
		InputStream inputstream = null;
		byte[] rece = new byte[1000];
		try {
			ServerSocket serversocket = new ServerSocket(8888); // 服务器的套接字,端口为8888
			while (true) {
				socket = serversocket.accept();
				inputstream = socket.getInputStream();// 得到输入流
				outputstream = socket.getOutputStream();// 得到输出流
				len = inputstream.read(rece);// 接受客户端消息
				if (len != 0)
					reces = new String(rece, 0, len);
				System.out.println(reces);
				BufferedReader bufferreader = new BufferedReader(new InputStreamReader(System.in));
				outputstream.write(("服务器....." + bufferreader.readLine()).getBytes());// 返回给客户端的欢迎信息
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			inputstream.close();
			outputstream.close();
			socket.close();// 记住一定要关闭这些输入,输出流和套接字
		}

	}
}

客户端程序:

package com.michael.contact;

import java.io.BufferedReader;  
import java.io.IOException;  
import java.io.InputStream;  
import java.io.InputStreamReader;  
import java.io.OutputStream;  
import java.net.InetAddress;  
import java.net.Socket;  
public class Client {  
    public static void main(String[] args) throws IOException {  
        String ServerIpAddress = "127.0.0.1";// 对方的IP  
        String words = "Hello";// 对方的话  
        String temp = null;  
        byte[] serverSay = new byte[1000];// 读取<1KB  
        InputStreamReader read = null;  
        int len = 0;  
        Socket s = null;  
        OutputStream outputstream = null;  
        InputStream inputstream = null;  
        // 取得你给他的IP  
        System.out.println("请输入对方的IP(默认发给自己):");  
        read = new InputStreamReader(System.in);  
        try {  
            temp = new BufferedReader(read).readLine();  
            if (!temp.equals(""))  
                ServerIpAddress = temp;  
        } catch (IOException e1) {  
        }  
        // 取得你各他的Message  
        System.out.println("请输入想对他(她)说的话----输入QUIT 退出!!:");  
        read = new InputStreamReader(System.in);  
        while (true) {  
            try {  
                temp = "Hello";  
                temp = new BufferedReader(read).readLine();  
                words = temp;  
                if (words.equals("quit"))  
                    break;  
                temp = "客户端....." + words;  
                // 发起通信  
                s = new Socket(InetAddress.getByName(ServerIpAddress), 8888);// 套接字的IP地址和端口号  
                outputstream = s.getOutputStream();  
                inputstream = s.getInputStream();  
                outputstream.write(temp.getBytes("gbk"));// 向服务器发送消息  
                len = inputstream.read(serverSay);// 接受服务器消息  
                System.out.println( new String(serverSay, 0, len));// 客户端控制台显示服务器返回的信息  
            } catch (Exception e) {  
            } finally {  
                inputstream.close();  
                outputstream.close();  
                s.close();// 记住一定要关闭这些输入,输出流和套接字  
            }  
        }// end while  
    }  
}  


Java socket模拟聊天程序