TCP Transport:
Service side: ServerSocket
Has client: Socket
One server can have multiple clients
The getInputStream and Getoutputstream methods of the Scoket object can be used to obtain the communication IO flow between the scoket.
Package network programming. UDP network programming; import Java.net.datagrampacket;import Java.net.datagramsocket;public class Udprtest {/** * @param args * @throws Exception */public static void Main (string[] args) throws Exception {//Create UDP service Datagramsocket ds = new Datagramsocket ( 10005);//The port number that the receiver listens to//defines the data format byte[] buf = new byte[1024];D atagrampacket dp = new Datagrampacket (buf, buf.length);// Receive data while (true) {ds.receive (DP); String IP = dp.getaddress (). gethostaddress (); String data = new String (Dp.getdata (), 0,dp.getlength ()); int port = Dp.getport (); System.out.println (ip+ ":" +port+ ":" +data);}}}
Package network programming. UDP network programming; import Java.net.datagrampacket;import Java.net.datagramsocket;import Java.net.inetaddress;public class udpnettest {public static void main (string[] args) throws exception{//create UDP service Datagramsocket ds = new Datagramsocket (8889) //UDP the port number of the sender//create the data to be sent byte[] buf = "Hello world! ". GetBytes ();D atagrampacket dp = new Datagrampacket (buf, Buf.length, Inetaddress.getbyname (" localhost "), 10005);// Address to each other and port number received by the other//send while (true) {Thread.Sleep (+);d S.send (DP);}}
Package network programming. Multithreading and UDP implement chat function; import Java.io.bufferedreader;import Java.io.inputstreamreader;import Java.net.datagrampacket;import java.net.datagramsocket;import java.net.inetaddress;/** * Chat room: A thread control receiving data, a line programmed to send data * Since the data is received and sent inconsistent, all to define two run methods, so to define two * @author Administrator * */public class Chatroom {public static void main (string[] args ) throws Exception{datagramsocket Sendds = new Datagramsocket ();D atagramsocket Recds = new Datagramsocket (9990); Send send = new Send (SENDDS); Receiver RE = new receiver (RECDS), new Thread (send), start (); new Thread (re). Start ();}} Class Send implements Runnable{private Datagramsocket ds; Send (Datagramsocket ds) {this.ds = ds;} public void Run () {try {bufferedreader br = new BufferedReader (new InputStreamReader (system.in)); String line = null;while (line = Br.readline ())!=null) {//while loop, which guarantees that this thread does not die under certain circumstances if ("886". Equals (line)) break;byte[] Buff = line.getbytes ();D atagrampacket dp =new datagrampacket (Buff, buff.length, Inetaddress.getbyname ("192.168.0.255" ), 9990);d S.send (dp);}} catch (Exception e) {throw new RuntimeException ("Send-side exception");}}} Class Receiver implements Runnable{private Datagramsocket ds; Receiver (Datagramsocket ds) {this.ds = ds;} public void Run () {try {while (true) {//while loop, ensure this thread does not die byte[] buf = new byte[1024];D atagrampacket dp = new Datagrampacket (bu F, Buf.length);d s.receive (DP); String IP = dp.getaddress (). gethostaddress (); int port = Dp.getport (); String data = new String (Dp.getdata (), 0, Dp.getdata (). length); System.out.println (ip+ ":" +port+ "said:" +data);}} catch (Exception e) {throw new RuntimeException ("Receive failed");}}}