Java Fundamentals-Network programming ——— Building Chat forms

Source: Internet
Author: User



Simple presentation of receivers and transmitters




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, only the pipe is sent. Regardless of whether the receiving can receive the data, it should start the receiving Terminal program, and then start the sender program */}}

Two-form 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-form 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 Form 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 ();}}














Java Fundamentals-Network programming ——— Building Chat forms

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.