13 java里的tcp网络编程

在java里把tcp服务器的功能封装成ServerSocket类,在服务器端通过Socket类对象与连接上来的客户端通信。而且客户端也是使用Socket类对象连接服务器端及通信的.

服务器端:

1 创建一个ServerSocket类对象

        ServerSocket srv = null; //声明ServerSocket指针,用于存放下面创建出来的ServerSocket对象地址
        try {
            srv = new ServerSocket(7788); //创建ServerSocket对象时指定绑定端口号7788
        } catch (IOException e) {  //因端口号可能会发生重用的状况,所以有可能会抛出异常,所以得用try ... catch处理异常
           //   ...
        }

2 调用服务器对象的accept函数成员,等待客户端的连接,每个客户端连接上来会就会得到一个Socket对象,服务器端就用这个Socket对象专用于与客户端的通信使用.

            Socket s; //声明一个Socket指针,用于存放客户端连接上来时得到的Socket对象地址

            s = srv.accept(); //调用此函数会进入堵塞,直到有客户端连接上来.
            // String ip = s.getInetAddress().toString(); //获取s指向的Socket对象的客户端的ip地址
            // int port = s.getPort(); //获取出客户端的端口号

3 当客户端连接上来得到Socket对象地址后,在服务器端就可以通过Socket对象与客户端通信了.

//接收客户端数据
        InputStream in;
        byte [] data = new byte[100];

        in = s.getInputStream(); //获取Socket对象的输入流
        ret = in.read(data); //等待接收数据直到客户端发数据过来为止
        System.out.println(new String(data)); //打印出数据
        in.close(); //关闭输入流


//发数据到客户端
        OutputStream out = s.getOutputStream(); //获取Socket对象的输出流
        out.write("hello world".getBytes()); //通过输出流发数据到客户端
        out.close(); //关闭输出流


客户端:

1 创建一个Socket对象,并指定连接服务器的ip地址及端口号

        Socket s = null; //声明一个Socket指针用于存放创建出来的Socket对象
        try {
            s = new Socket("192.168.250.250", 7788); //创建Socket对象并指定连接服务器的ip地址及端口号
        } catch (Exception e) { //因连接服务器有连接失败的可能,所以需要处理有可能会抛出的异常
            // ...
        }

2 连接成功后就可以获取Socket对象的输入流和输出流,用于与服务器端通信使用了.

//接收服务器端数据
        InputStream in;
        byte [] data = new byte[100];

        in = s.getInputStream(); //获取Socket对象的输入流
        ret = in.read(data); //等待接收数据直到服务器端发数据过来为止
            ...

//发数据到服务器端
        OutputStream out = s.getOutputStream(); //获取Socket对象的输出流
        out.write("hello world".getBytes()); //通过输出流发数据到服务器端
           ...


实现一个简单的tcp通信例子, 客户端连接上服务器端后发出一个字符串,服务器端打印输出接收到的内容:
服务器端代码:

/* Main.java */

import java.io.IOException;
import java.io.InputStream;
import java.net.*;

public class Main {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        ServerSocket srv = null;

        try {
            srv = new ServerSocket(7788);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("bind failed");
            System.exit(0);
        }

        Socket s;
        String ip;
        int port, ret;
        InputStream in;
        byte [] data = new byte[100];
        while (true) {
            s = srv.accept();
            ip = s.getInetAddress().toString();
            port = s.getPort();
            System.out.println("connection from " + ip + " " + port);

            in = s.getInputStream();
            while (true) {
               ret = in.read(data);
               if (ret<= 0)
                   break;
               System.out.println(new String(data));

            }
            s.close();
        }

    }

}

客户端代码:

/* Main.java */

import java.io.IOException;
import java.io.OutputStream;
import java.net.*;

public class Main {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        Socket s = null;

        try {
            s = new Socket("192.168.250.250", 7788);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }

        OutputStream out = s.getOutputStream();

        out.write("hello world".getBytes());

        out.close();
        s.close();


    }

}


在tcp网络里,每个客户端只能与服务器端通信,当需要与其它客户端通信时就得依靠服务器端转发才可以。

如实现一个群发的tcp网络服务器,当服务器接收到任意一个客户端的数据时都会转发到所有的客户端去。

因每个客户端连接上来时得到一个Socket对象,多个客户端连接时就会有多个对象,而且数据个数是不确定的,所以用LinkedList容器对象来存放所有客户端连接到得的Socket对象.

而且在通过Socket对象的输入流接收数据会堵塞,为了能同时接收多个客户端的数据,所以每个客户端连接上来时就会一个子线程专用于接收数据使用.

import java.io.IOException;
import java.io.InputStream;
import java.net.*;
import java.util.Iterator;
import java.util.LinkedList;

class MyThread implements Runnable {
    private Socket s;
    LinkedList clients;
    public MyThread(Socket s, LinkedList clients) {
        this.s = s;
        this.clients = clients; 

        System.out.println("connection from " + s.getInetAddress().toString());
        new Thread(this).start();
    }

    public void run() {
        InputStream in = null;

        try {
            in = s.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        ///////////
        byte [] data = new byte[1500];
        int ret = 0;
        Socket stmp;
        String str;

        while (true) {
            try {
                ret = in.read(data);
            } catch (IOException e) {
                e.printStackTrace();
            }

            if (ret <= 0)
                break;

            str = s.getInetAddress().toString() + " :" + s.getPort() + " " + new String(data, 0, ret);
            System.out.println(str);        
            //转发到所有客户端
            Iterator it = clients.iterator();
            while (it.hasNext()) {
                stmp = (Socket) it.next();
                try {
                    System.out.println("write to " + stmp.getInetAddress());
                    stmp.getOutputStream().write(str.getBytes());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }   
            }
        }
        clients.remove(s);  
        try {
            s.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

public class Main {
    public static void main(String[] args) throws IOException {
        LinkedList clients = new LinkedList();
        ServerSocket srv = new ServerSocket(7788);
        Socket s = null;

        while (true) {
            s = srv.accept();
            if (s.isConnected()) {
                clients.add(s);
                new MyThread(s, clients);
            }
        }


    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值