Java in the network programming is mainly Java socket programming, belongs to the advanced part of Java EE, the following is a summary of the programming of the network, the code is compiled debugging
c/S Program application: Client/server mode, such as QQ client, client connected to the server, a C/S mode application must have two sets of programs, one is the client's program, one is the server program.
b/S application: Browser/server mode, such as the current various sites are B/s mode, all the program code is on the server, the user through the browser to access.
The C/S program is divided into two types:
TCP-based protocol: socket (socket), reliable programming: A->b make a connection if you call first
Based on UDP protocol: unreliable, such as SMS function.
If you write a TCP program that requires two packages of Java support:
JAVA.NET.*: mainly provide network support;
|-serversocket class: Server-side programs
|-socket Class: Client program
java,io.*: Passing Information flow
Two features of the client:
1. Create a socket
2. Receive input commands (input stream)--the program that is transmitted over the network is a byte stream.
The following is the server side, the client's two case program, the server side wants the client output Hello Word, the client receives and displays;
Server program:
1 Import java.io.*; 2 Import java.net.*; 3 public class Tcpsever {4 5 /** 6 * @param args 7 * @throws IOException 8 */9 public static Voi D main (string[] args) throws IOException {ten //TODO auto-generated method STUB11 //Use ServerSocket12 ServerSocket Server = new ServerSocket (8000); //Each user is a Socket15 Socket client in the program = null;16 +// waiting for client to connect to the customer = Server.accept ();/ /Like client printing information printwriter out = null;22 The information is printed to the client on the new PrintWriter (Client.getoutputstream ()); out.println ("Hello World"); 26 out.close (); client.close (); server.close (); }30 31}
Client program:
1 package Client; 2 Import java.io.*; 3 Import java.net.*; 4 5 public class TCPClient {6 7 /** 8 * @param args 9 * @throws IOException10 * @throws Unknownho StException11 */12 public static void Main (string[] args) throws Unknownhostexception, IOException { // TODO auto-generated Method Stub14 //Represents a client's Socket15 Socket client = null;16 +/ /Indicates input information for a client 18 bufferedreader buf = null;19 client = new Socket ("localhost", 8000), buf = new BufferedReader ( New InputStreamReader (Client.getinputstream ())); System.out.println (Buf.readline ()); Buf.close (); client.close (); }26 27}
Two classes dedicated to implementing UDP are also prepared in the JDK
1.DatagramScoket
2.DatagramPacket
Server-side programs:
Package Client;import Java.io.*;import java.net.*; public class TCPClient { /** * @param args * @throws ioexception * @throws unknownhostexception */< C6/>public static void Main (string[] args) throws Unknownhostexception, IOException { //TODO auto-generated Method St UB //Indicates a client's socket socket clients = NULL; Represents the input information for a client bufferedreader buf = null; Client = new Socket ("localhost", 8000); BUF = new BufferedReader (New InputStreamReader (Client.getinputstream ())); System.out.println (Buf.readline ()); Buf.close (); Client.close (); } }
Client program:
1 package Test4; 2 Import java.io.*; 3 Import java.net.*; 4 public class UdpClient {5 6 /** 7 * @param args 8 * @throws IOException 9 */10 public static VO ID Main (string[] args) throws IOException {one //TODO auto-generated method stub12 datagramsocket ds = null;13
datagrampacket DP = null;14 byte[] b = new byte[1024];16 -ds = new Datagramsocket (8000); 18
DP = new Datagrampacket (b,b.length), ds.receive (DP), string str = new String (Dp.getdata (), 0, Dp.getlength ()); System.out.println (str); }23 24}
Network Programming in Java