第一句子网 - 唯美句子、句子迷、好句子大全
第一句子网 > JAVA之socket编程服务器与客户端通信--实现简易聊天室

JAVA之socket编程服务器与客户端通信--实现简易聊天室

时间:2019-07-27 14:48:37

相关推荐

JAVA之socket编程服务器与客户端通信--实现简易聊天室

本文将介绍TCP和UDP两种服务器与客户端之间的通信协议

1、首先介绍TCP和UDP分别是什么:TCP(Transfer Control Protocol) 是传输控制协议的缩写,被称 TCP / IP。UDP (User Datagram Protocol)是用户数据报协议的缩写,一个无连接的协议。

2、本文用到的socket是什么:socket套接字提供了两台计算机之间的通信机制。 客户端程序创建一个套接字,并尝试连接服务器的套接字。

3、本文主要内容:

服务器端ServerSocket 对象建立方法:new ServerSocket(2000);端口号为2000服务器端Socket对象建立方法:ServerSocket对象调用accept()方法利用Socket对象获得输入流:getInputStream()利用输入流获得Scanner输入对象:Scanner(s.getInputStream())客户端连接服务器方法:Socket(“127.1.1.1”,2000); 给Socket传递两个参数分别是服务器的IP与端口号利用Socket对象获得输出流:getOutputStream()利用输出流对象进行流输出对象:PrintStream(s.getOutputStream())

一、TCP/IP协议信息的发送和接受

1、服务器一次性向客户端发送信息

服务器service代码:

import .*;import java.io.*;class service{public static void main(String[] args)throws Exception{ServerSocket ss=new ServerSocket(2000);Socket s=new Socket();while(true){s=ss.accept();PrintStream ps=new PrintStream(s.getOutputStream());ps.println("hello world");}}}

客户端client代码:

import .*;import java.util.*;class client{public static void main(String[] args)throws Exception{Socket s=new Socket("127.1.1.1",2000);Scanner sc=new Scanner(s.getInputStream());System.out.println(sc.nextLine());}}

2、服务器连续向客户端发送数据,当键入quit结束信息的发送

服务器service代码

import .*;import java.io.*;import java.util.*;class server{Socket s;server(Socket s) throws Exception{this.s=s;PrintStream ps=new PrintStream(s.getOutputStream());Scanner sc=new Scanner(System.in);while(sc.hasNext()){String str=sc.nextLine();if(str.equals("quit"))System.exit(1);ps.println(str);}}public static void main(String[] args) throws Exception{ServerSocket ss=new ServerSocket(2000);while(true){new server(ss.accept());}}}

客户端client代码

import .*;import java.io.*;import java.util.*;class client{client() {try{Socket s=new Socket("127.1.1.1",2000);Scanner sc=new Scanner(s.getInputStream());while(sc.hasNext()){String str=sc.nextLine();if (str.equals("quit"))System.exit(1);System.out.println(str);}}catch(Exception e){}}public static void main(String[] args){client sc=new client();}}

3、客户端连续向服务器发送信息,键入quit结束发送

服务器server代码

import .*;import java.io.*;import java.util.*;class server{server() {try{ServerSocket ss=new ServerSocket(2000);Socket s=new Socket();Scanner sc=new Scanner(s.getInputStream());while(sc.hasNext()){s=ss.accept();String str=sc.nextLine();if(str.equals("quit"))System.exit(1);System.out.println(str);}}catch(Exception e){}}public static void main(String[] args){server1 sc=new server();}}

客户端client代码

import .*;import java.io.*;import java.util.*;class client{client() {try{Socket s=new Socket("127.1.1.1",2000);PrintStream ps=new PrintStream(s.getOutputStream());Scanner sc=new Scanner(System.in);while(sc.hasNext()){String str=sc.nextLine();if (str.equals("quit"))System.exit(1);ps.println(str);}}catch(Exception e){}}public static void main(String[] args){client sc=new client();}}

4、服务器端与客户端各利用两个线程分别用于发送和接收,从而实现双向信息接收,键入“exit”退出程序。

服务器service代码

import java.io.*;import java.util.*;import .*;class servicereceive extends Thread{Socket s;ServerSocket ss;Scanner sc;public void run(){try{ServerSocket ss=new ServerSocket(2001);Socket s=new Socket();while(true){s=ss.accept();Scanner sc=new Scanner(s.getInputStream());while(sc.hasNext()){String str=sc.nextLine();if (str.equals("exit"))System.exit(1);System.out.println("receive:"+str);}System.exit(1);}}catch(Exception e){}}}class servicesend extends Thread{ServerSocket ss;Socket s;PrintStream ps;Scanner sc;public void run(){try{ServerSocket ss=new ServerSocket(2000); Socket s=new Socket();Scanner sc=new Scanner(System.in);while(true){s=ss.accept();PrintStream ps=new PrintStream(s.getOutputStream());while(sc.hasNext()){String str=sc.nextLine();if(str.equals("exit"))System.exit(1);ps.println(str);System.out.println("send:"+str);}}}catch(Exception e){}}}class service{public static void main(String[] args) throws Exception{new servicereceive().start();new servicesend().start(); }}

客户端client代码

import java.io.*;import java.util.*;import .*;class clientreceive extends Thread{Socket s;Scanner sc;public void run(){try{Socket s=new Socket("127.1.1.1",2000);Scanner sc=new Scanner(s.getInputStream());while(sc.hasNext()){String str=sc.nextLine();if (str.equals("exit"))System.exit(1);System.out.println("receive:"+str);}}catch(Exception e){}}}class clientsend extends Thread{Socket s;PrintStream ps;Scanner sc;public void run(){try{Socket s=new Socket("127.1.1.1",2001);PrintStream ps=new PrintStream(s.getOutputStream());Scanner sc=new Scanner(System.in);while(sc.hasNext()){String str=sc.nextLine();if (str.equals("exit"))System.exit(1);ps.println(str);System.out.println("send:"+str);}}catch(Exception e){}}}class client{public static void main(String[] args) throws Exception{new clientreceive().start();new clientsend().start(); }}

5、服务器向多个客户端发送信息

带线程的类的建立:class multitalk extends Thread多线程对象的建立:multitalk(ss.accept())重写抽象方法:public void run()

服务器端代码

import .*;import java.util.*;import java.io.*;class multitalk extends Thread{Socket s;multitalk(Socket s){this.s=s;}public void run(){try{PrintStream ps=new PrintStream(s.getOutputStream());Scanner sc=new Scanner(System.in);while(sc.hasNext()){String str=sc.nextLine();ps.println(str);if(str.equals("bye")){System.exit(1);}}}catch(Exception e){}}public static void main(String[] args) throws Exception{ServerSocket ss=new ServerSocket(2000);while(true){multitalk mt=new multitalk(ss.accept());mt.start();}}}

客户端代码

import java.util.*;import java.io.*;import .*;class multiclient{multiclient(){try{Socket s=new Socket("127.1.1.1",2000);Scanner sc=new Scanner(s.getInputStream());while(sc.hasNext()){String str=sc.nextLine();System.out.println(str);if(str.equals("bye"))System.exit(1);}}catch(Exception e){}}public static void main(String[] args){new multiclient();}}

二、UDP协议信息的发送和接收

主要内容

数据报socket类对象的建立:DatagramSocket(port)数据报对象的建立:DatagramPacket(b,b.length)或DatagramPacket(b,0,b.length,InetAddress.getByName(ip),port)数据报socket类对象接收或发送数据报对象ds.receive(indp);ds.send(outdp);DatagramSocket(port)

1、服务器接收客户端信息

服务端和客户端公共类udpmessage

import .*;class udpmessage{DatagramPacket indp, outdp;DatagramSocket ds;byte[] b;udpmessage(int port) throws Exception{ds=new DatagramSocket(port);}void sendmessage(String ip,int port) throws Exception{b=new byte[32];String str="hello from send";b=str.getBytes();outdp=new DatagramPacket(b,0,b.length,InetAddress.getByName(ip),port);ds.send(outdp);}void receivemessage() throws Exception{b=new byte[32];while(true){indp=new DatagramPacket(b,b.length);ds.receive(indp);System.out.println(new String(b,0,b.length));}}}

发送端代码

class sendmessage{public static void main(String[] args) throws Exception{udpmessage send=new udpmessage(4000);send.sendmessage("127.1.1.1",2000);}}

接收端代码

class receivemessage{public static void main(String[] args) throws Exception{udpmessage receive=new udpmessage(2000);receive.receivemessage();}}

2、服务器端向客户端组播信息

主要内容

数据报类:DatagramPacket(b,0,b.length,group,port)组播类: 发送MulticastSocket() 接收MulticastSocket(port);组播地址:group=InetAddress.getByName(ip);加入组播组:joinGroup(group)

服务端和客户端公共类udpmessage

import java.util.*;import .*;class udpcast{InetAddress group;MulticastSocket ss,cs;int port=2000;DatagramPacket indp,outdp;udpcast(String ip) throws Exception{group=InetAddress.getByName(ip);}void send() throws Exception{byte[] buff;Scanner sc =new Scanner(System.in);ss=new MulticastSocket();ss.joinGroup(group);while(sc.hasNext()){String str=sc.nextLine();buff=str.getBytes();outdp=new DatagramPacket(buff,0,buff.length,group,port);ss.send(outdp);if(str.equals("quit"))System.exit(0);}}void receive()throws Exception{cs=new MulticastSocket(port);cs.joinGroup(group);while(true){byte[] buff=new byte[32];indp=new DatagramPacket(buff,0,buff.length,group,port);cs.receive(indp);String str=new String(buff,0,buff.length);System.out.println(str);if(str.trim().equals("quit"))System.exit(0);}}}

发送端

class udpcastserver{public static void main(String[] e) throws Exception{new udpcast("224.0.0.0").send();}}

接收端

class udpcastclient{public static void main(String[] e) throws Exception{new udpcast("224.0.0.0").receive();}}

以上就是本文所有内容,希望能帮到大家!!!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。