基于Socket的java网络编程(实现类似于QQ两人聊天的交互)

来源:互联网 发布:湍流问题知乎 编辑:程序博客网 时间:2024/06/12 01:12
基于Socket的java网络编程

1,什么是Socket

网络上的两个程序通过一个双向的通讯连接实现数据的交换,这个双向链路的一端称为一个Socket。Socket通常用来实现客户方和服务方的连接。Socket是TCP/IP协议的一个十分流行的编程界面,一个Socket由一个IP地址和一个端口号唯一确定。但是,Socket所支持的协议种类也不光TCP/IP一种,因此两者之间是没有必然联系的。在Java环境下,Socket编程主要是指基于TCP/IP协议的网络编程。

2,Socket通讯的过程

Server端Listen(监听)某个端口是否有连接请求,Client端向Server 端发出Connect(连接)请求,Server端向Client端发回Accept(接受)消息。一个连接就建立起来了。Server端和Client 端都可以通过Send,Write等方法与对方通信。

对于一个功能齐全的Socket,都要包含以下基本结构,其工作过程包含以下四个基本的步骤:

  (1) 创建Socket;
  (2) 打开连接到Socket的输入/出流;
  (3) 按照一定的协议对Socket进行读/写操作;
  (4) 关闭Socket.(在实际应用中,并未使用到显示的close,虽然很多文章都推荐如此,不过在我的程序中,可能因为程序本身比较简单,要求不高,所以并未造成什么影响。)
 

3,今天写了一个运用socket实现类似于QQ两个人交互信息的例子,写的不是很完善,有不好的地方希望能指出来。

/***socket 服务端代码*/package cjy.com;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 SocketServer {ServerSocket serverSocket = null;Socket socket = null;public void sockets() {try {serverSocket = new ServerSocket(8888);System.out.println("服务器开启。。。");int i = 0;// 实现多个客户端连接while (true) {socket = serverSocket.accept();System.out.println("客户端" + i + "连接成功。。。");if(socket!=null){Thread thread = new Thread(new LogicThread(socket));thread.setDaemon(true);thread.start();i++;}}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}private class LogicThread implements Runnable {Socket socket = null;InputStream in = null;public LogicThread(Socket socket) {this.socket = socket;}public void run() {// TODO Auto-generated method stubSystem.out.println(socket.getInetAddress());try {in = socket.getInputStream();byte[] b = new byte[1024];// 实现一次连接多次通话while (true) {// 把接受数据写入线程new Thread() {public void run() {while (true) {byte[] b = new byte[1024];int n;try {n = in.read(b);// 属于阻塞方法System.out.println("接受数据:"+ new String(b, 0, n));} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();try {socket.close();break;} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}}}}}.start();// 发送数据InputStreamReader input = new InputStreamReader(System.in);// 属于阻塞方法BufferedReader br = new BufferedReader(input);String outN;try {outN = br.readLine();socket.getOutputStream().write(outN.getBytes());System.out.println("服务端已发送");} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}if(socket.isClosed()){break;}}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}public static void main(String[] args) {new SocketServer().sockets();}}/***客户端程序*/package cjy.com;import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.net.Socket;import java.net.UnknownHostException;public class SocketClient {Socket socket=null;InputStreamReader input = null;InputStream in = null;OutputStream out = null;byte[] b = new byte[1024];/** * @param args */public  void socketStart(){try {socket = new Socket("服务器端ip地址",8888);out = socket.getOutputStream();while(true){input = new InputStreamReader(System.in);String name = new BufferedReader(input).readLine();out.write(name.getBytes());if("bye".equals(name)){break;}System.out.println("已发送");//接受返回数据new Thread(){public void run(){try {while(true){in =socket.getInputStream();int n = in.read(b);System.out.println("返回数据:"+new String(b,0,n) );}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}.start();}} catch (UnknownHostException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{try {//关闭流和连接in.close();out.close();socket.close();   } catch (Exception e2) {}}}public static void main(String[] args) {// TODO Auto-generated method stubnew SocketClient().socketStart();}}



原创粉丝点击