Java Network Programming

Source: Internet
Author: User

The end of the paper is shallow

Network programming involves a lot of content, it and multi-threaded, I/O is the core of Java , Java is also a good part of the production. Before using the C and Libpcap library to do a grab packet small program to be tired, just the choice of network card, binding and connection is very laborious to achieve a C/s simple communication connection is very troublesome, mainly can consider too much, now see Java has more advanced interface, There is no need to pay attention to a lot of things (now it seems that the dead knock with those things really meaningless), to achieve a simple communication simulation is in too simple and convenient:

1.TCP mode:

The client transmits a picture to the Server,server save and returns a message to the client side:

<pre name= "code" class= "java" >package roadarchitectweb.test;import Java.io.bufferedinputstream;import Java.io.file;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.FileOutputStream; Import Java.io.ioexception;import Java.io.inputstream;import Java.io.outputstream;import Java.io.unsupportedencodingexception;import Java.net.inetaddress;import Java.net.serversocket;import Java.net.socket;import Java.net.unknownhostexception;import Java.nio.channels.serversocketchannel;import Org.junit.test;import SUN.PRINT.RESOURCES.SERVICEUI;/*TCP Socket Network Programming */public class Test11 {@Testpublic void client () { FileInputStream FileInputStream = Null;outputstream OutputStream = Null;inputstream InputStream = null; Socket socket = null;try {FileInputStream = new FileInputStream (New File ("hello1.jpg")); InetAddress inetaddress = Inetadd Ress.getbyname ("127.0.0.1"); socket = new socket (inetaddress,8980); outputstream = Socket.getoutputstream (); InputStream = Socket.getinputstream (); byte[] B = new Byte[1024];int Length;while (fileinputstream.available ()!=0 && (Length=fileinputstream.read (b) )! =-1) {Outputstream.write (new String (b, 0, length). GetBytes ()); if (FileInputStream! = null) try {fileinputstream.close ();} catch (IOException e) {e.printstacktrace ();} System.out.println ("Test11.client () write"); while (Inputstream.available ()!=0 && (Length=inputstream.read ( b)) {!=-1) {System.out.println ("test11.client ():" +new String (B,0,length));}} catch (FileNotFoundException e) {e.printstacktrace ();} catch (Unknownhostexception e) {e.printstacktrace ();} catch ( Unsupportedencodingexception e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} finally {if (outputstream! = null) try {outputstream.close ();} catch (IOException e) {e.printstacktrace ();} if (InputStream! = null) try {inputstream.close ();} catch (IOException e) {e.printstacktrace ();} if (socket!=null) try {socket.close ();} catch (IOException e) {e.printstacktrace ();}}} @Testpublic void Server () {FileouTputstream FileOutputStream = Null;inputstream InputStream = Null;outputstream outputstream = null; Socket socket = NULL; ServerSocket ServerSocket = null;try {serversocket = new ServerSocket (8980); socket = Serversocket.accept (); inputStream = Socket.getinputstream (); fileoutputstream = new FileOutputStream ("hello2.jpg"); byte[] B = new Byte[1024];int length; while (Inputstream.available ()!=0 && (length = Inputstream.read (b))! =-1) {length = Inputstream.read (b); Fileoutputstream.write (b, 0, length);} if (FileOutputStream! = null) try {fileoutputstream.close ();} catch (IOException e) {e.printstacktrace ();} System.out.println ("Receive picture"); outputstream = Socket.getoutputstream (); Outputstream.write ("Server returns client Information". GetBytes ());} catch (Unknownhostexception e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} finally {if (outputstream! = null) try {outputstream.close ();} catch (IOException e) {e.printstacktrace ();} if (InputStream! = null) try {inputstream.close ();} catch (IOException e) {e.Printstacktrace ();} if (socket!=null) try {socket.close ();} catch (IOException e) {e.printstacktrace ();} if (serversocket!=null) try {serversocket.close ();} catch (IOException e) {e.printstacktrace ();}}}}

2.UDP mode:

The client sends a message (like a return message, does not do):

Package Roadarchitectweb.test;import Java.io.ioexception;import Java.net.datagrampacket;import Java.net.datagramsocket;import Java.net.inetaddress;import Java.net.socketexception;import Java.net.unknownhostexception;import Org.junit.test;public class Test12 {@Testpublic void client () {Datagramsocket datagramsocket= null;try {byte[] buf = "Client Sent". GetBytes ();D atagrampacket datagrampacket = new Datagrampacket (buf,0, Buf.length,inetaddress.getbyname ("127.0.0.1"), 9390);d atagramsocket = new Datagramsocket ();d atagramsocket.send ( Datagrampacket); System.out.println ("Test12.client (): Already sent");} catch (Unknownhostexception e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} finally {if (datagramsocket!=null) {datagramsocket.close ();}}} @Testpublic void Server () {datagramsocket datagramsocket= null;try {byte[] buf = new Byte[30];D Atagrampacket Datagrampacket = new Datagrampacket (buf, 0, buf.length);d atagramsocket = new Datagramsocket (9390); Datagramsocket.receive (Datagrampacket); System.oUt.println ("Test12.server ():" +new String (Datagrampacket.getdata (), 0,datagrampacket.getlength ()));} catch (SocketException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} finally {if (datagramsocket!=null) Datagramsocket.close ();}}}
This is just a high-level interface, more network programming after the summary

Java Network Programming

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.