欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java网络编程学习java聊天程序代码分享

程序员文章站 2024-02-19 22:53:46
复制代码 代码如下:package com.neusoft.edu.socket;import java.io.bufferedreader;import java.io....

复制代码 代码如下:

package com.neusoft.edu.socket;
import java.io.bufferedreader;
import java.io.ioexception;
import java.io.inputstreamreader;
import java.io.printwriter;
import java.net.serversocket;
import java.net.socket;
/**
 * 服务器端代码
 * 获取客户端发送的信息,显示并且返回对应的回复
 * 1、创建serversocket对象
 * 2、调用accept方法获取客户端连接
 * 3、使用输入流读取客户端发送的数据
 * 4、使用输出流向客户端写入数据
 * 5、关闭对应的对象
 * @author l
 *
 */
public class chatserver {
    /**
     * @param args
     */
    public static void main(string[] args) {
        try {
            //1、创建serversocket对象,8875为自定义端口号
            serversocket server = new serversocket(8857);

            //简单提示
            system.out.println("等待客户端连接……");

            //2、获取客户端连接
            socket client = server.accept();

            //获取客户端的相关信息
            system.out.println(client.getinetaddress().gethostaddress() + "连接上来了……");

            //3.1、定义输入流和输出流对象
            bufferedreader in = new bufferedreader(
                    new inputstreamreader(
                            client.getinputstream()));

            //用来获取从控制台输入的数据,将该数据发送给客户端
            bufferedreader inbyserver = new bufferedreader(
                    new inputstreamreader(system.in));

            printwriter out = new printwriter(client.getoutputstream(), true);

            //读取到的数据
            string data = null;
            string answer = null;

            //循环和客户端进行通信
            do
            {
                //3.2、读取客户端发送的数据
                data = in.readline();

                //在服务器端显示读取到的数据
                system.out.println("客户端发送信息:" + data);

                //获取服务器端要发送给客户端的信息
                system.out.print("服务器端回复客户端:");
                answer = inbyserver.readline();

                //3.3、将数据写入到客户端
                out.println(answer);
                out.flush();
            }while(!"bye".equals(data));

            //4、关闭相关资源
            out.flush();
            in.close();
            inbyserver.close();
            out.close();

            //关闭socket对象
            client.close();
            server.close();

            system.out.println("服务器端关闭……");
        } catch (ioexception e) {
            // todo auto-generated catch block
            e.printstacktrace();
        }
    }
}

复制代码 代码如下:

package com.neusoft.edu.socket;

import java.io.bufferedreader;
import java.io.ioexception;
import java.io.inputstreamreader;
import java.io.printwriter;
import java.net.socket;
import java.net.unknownhostexception;

/**
 * 聊天客户端
 * 1、创建socket对象
 * 2、写入数据
 * 3、读取从服务器端发来的数据
 * 4、关闭
 * @author l
 *
 */
public class chatclient {

    /**
     * @param args
     */
    public static void main(string[] args) {
        try {
            //1、创建socket对象,("192.168.1.107", 8857)分别为主机ip和端口号,两个类端口号要一致
            socket client = new socket("192.168.1.107", 8857);

            //2.1、创建输入流和输出流对象
            bufferedreader in = new bufferedreader(
                    new inputstreamreader(
                            client.getinputstream()));

            printwriter out = new printwriter(client.getoutputstream(), true);

            bufferedreader inbyclient = new bufferedreader(
                    new inputstreamreader(system.in));

            //服务器端发送的数据
            string data = null;
            //客户端向服务器端写入的数据
            string answer = null;

            do
            {
                //2.2、客户端的读写操作
                system.out.print("你说:");
                //获取要发送给服务器端的数据
                answer = inbyclient.readline();
                //向服务器端写入数据
                out.println(answer);
                out.flush();

                //获取服务器端发送的数据
                data = in.readline();
                //输出从服务器端获取的数据
                system.out.println("服务器端返回信息是:" + data);
            }while(!"bye".equals(data));

            //3、关闭
            in.close();
            out.close();
            inbyclient.close();
            client.close();

            system.out.println("客户端关闭……");
        } catch (unknownhostexception e) {
            // todo auto-generated catch block
            e.printstacktrace();
        } catch (ioexception e) {
            // todo auto-generated catch block
            e.printstacktrace();
        }
    }

}