A simple demo of the receiving and sending side
Import Java.io.*;import java.net.*;p ublic class Socketdemo {public static void main (string[] args) throws Exception {//To Do auto-generated method StubSystem.out.println ("Sender-side start"); System.out.println (Inetaddress.getlocalhost (). gethostaddress ());D atagramsocket ds = new Datagramsocket (); String str = "Send end came out"; byte[] buf = str.getbytes ();D atagrampacket dp = new Datagrampacket (Buf,buf.length, Inetaddress.getbyname ("192.168.3.4"), 10101);d S.send (DP);d s.close ();}}
Import Java.net.datagrampacket;import Java.net.datagramsocket;import Java.net.socketexception;public class SocketDemo2 {public static void main (string[] args) throws Exception {//TODO auto-generated method stub SYSTEM.OUT.PR INTLN ("Receive-side start"); Datagramsocket ds = new Datagramsocket (10101); byte[] buf = new byte[1024]; Datagrampacket DP = new Datagrampacket (buf,buf.length); Ds.receive (DP); String IP = dp.getaddress (). gethostaddress (); int port = Dp.getport (); String text = new string (Dp.getdata (), 0,dp.getlength ()); System.out.println (ip+ " " +port); System.out.println (text); Ds.close (); / * * Because the UDP protocol transmits data, just send the data, regardless of whether the receiving can receive data, so should start the receiving Terminal program, and then start the sender program */}}
Two-window chat mode:
To establish the sending side:
Import Java.io.bufferedreader;import Java.io.inputstreamreader;import Java.net.datagrampacket;import Java.net.datagramsocket;import Java.net.inetaddress;import Java.net.socketexception;public Class socketchatdemoinput {public static void main (string[] args) throws Exception {//TODO auto-generated method Stubsystem.out . println ("Send-side start");D atagramsocket ds = new Datagramsocket (); BufferedReader bufr = new BufferedReader (new InputStreamReader (system.in)); String line = null;while (line = Bufr.readline ())!=null) {byte[] buf = line.getbytes ();D atagrampacket dp = new Datagrampack ET (Buf,buf.length,inetaddress.getbyname ("192.168.3.4"), 10102);d S.send (DP), if ("over". equals [line)] break; Ds.close ();}}
Set up the receiving end
Import Java.net.datagrampacket;import Java.net.datagramsocket;import Java.net.socketexception;public class socketchatoutput {public static void main (string[] args) throws Exception {//TODO auto-generated method stubSystem.out.pr Intln ("Receive side start");D atagramsocket ds = new Datagramsocket (10102), while (true) {byte[] buf = new byte[1024];D atagrampacket DP = New Datagrampacket (buf,buf.length);d s.receive (DP); String IP =dp.getaddress (). gethostaddress (); int inpor = Dp.getport (); String text = new String (Dp.getdata (), 0,dp.getlength ()); System.out.println (ip+ " " + inpor+ " \ n" +text);}}}
Single-window chat mode
Import Java.io.bufferedreader;import Java.io.ioexception;import Java.io.inputstreamreader;import Java.net.datagrampacket;import Java.net.datagramsocket;import Java.net.inetaddress;import Java.net.socketexception;class send implements Runnable{private datagramsocket ds;p ublic Send (datagramsocket ds) { this.ds= DS;} public void Run () {try{system.out.println ("send-side start"); BufferedReader bufr = new BufferedReader (new InputStreamReader (system.in)); String line = null;while (line = Bufr.readline ())!=null) {byte[] buf = line.getbytes ();D atagrampacket dp = new Datagrampack ET (Buf,buf.length,inetaddress.getbyname ("192.168.3.4"), 10103);d S.send (DP), if ("over". Equals (line)) { SYSTEM.OUT.PRINTLN ("Chat program exit"); Break;}} Ds.close ();} catch (Exception e) {System.out.println (e.tostring ());}}} Class Rece implements Runnable{datagramsocket DS; Rece (Datagramsocket ds) {this.ds = ds;} public void Run () {while (true) {System.out.println ("Receive side start"); byte[] buf = new byte[1024];D atagrampacket dp = new Datagrampacket (buf,buf.length); try { Ds.receive (DP);} catch (IOException e) {e.printstacktrace ();} String IP = dp.getaddress (). gethostaddress (); int port = Dp.getport (); String text = new String (Dp.getdata (), 0,dp.getlength ()); System.out.println (ip+ "" + port+ "\ n" +text);}}} public class Socketgroupchat {public static void main (string[] args) throws Exception {//single-window chat mode Datagramsocket send = new Datagramsocket ();D atagramsocket rece = new Datagramsocket (10103); Send s = new send (send); Rece r = new Rece (rece); new thread (s). Start (); new Thread (R). Start ();}}
Black Horse Programmer--establishment of network programming ——— Chat window