Fundamentals: Java Network programming

Source: Internet
Author: User

Creating a Web server with the TCP communication model

??

The main use of ServerSocket has been listening, here to put it in the while loop, in the loop to open a single thread

??

public class MyWebServer {

public static void Main (string[] args) throws IOException {

ServerSocket ss=new ServerSocket (80);

Socket Socket=null;

while ((Socket=ss.accept ())!=null) {

New Httpthread (socket). Start ();

}

Ss.close ();

}

}

??

Write what you want to do in the run method in the thread, and in the constructor, pass the Socket object in.

??

public class Httpthread extends Thread {

private socket socket;

Httpthread (socket socket) {

This.socket=socket;

}

public void Run () {

OutputStream Outputstream=socket.getoutputstream ();

PrintWriter pwriter=new PrintWriter (outputstream);

Pwriter.println ("

Pwriter.println ("<body>");

Pwriter.println ("hello! This is my page ");

Pwriter.println ("</body>");

Pwriter.println ("

Pwriter.flush ();

Pwriter.close ();

Socket.close ();

}

}

??

Create an instant chat software with UDP communication model

??

The main is to write a receive thread and a send thread, and then start the two threads in the main function OK

??

Class Sendthread extends Thread {

Private Datagramsocket Datagramsocket;

private int sendport;

Sendthread (int port, int sendport) {

Super ();

This.sendport = SendPort;

This.datagramsocket = new Datagramsocket (port);

}

public void Run () {

BufferedReader breader = new BufferedReader (new InputStreamReader (system.in));

string string = null;

while ((string = Breader.readline ()) = null) {

Datagrampacket DP = new Datagrampacket (String.getbytes (), 0,string.length (), Inetaddress.getbyname ("localhost"), SendPort);

Datagramsocket.send (DP);

System.out.println ("Send Message:" + string);

}

}

}

Class Receivethread extends Thread {

Private Datagramsocket Datagramsocket;

Receivethread (int port) {

Super ();

This.datagramsocket = new Datagramsocket (port);

}

public void Run () {

byte[] buff = new byte[1024];

Datagrampacket DP = new Datagrampacket (buff, 1024);

while (true) {

Datagramsocket.receive (DP);

String string = new String (Dp.getdata (), 0, Dp.getlength ());

System.out.println ("Receive Message:" + string);

}

}

}

public static void Main (string[] args) throws SocketException {

New Receivethread (Integer.valueof (Args[0])). Start ();

New Sendthread (Integer.valueof (args[1]), integer.valueof (Args[2]). Start ();

}

??

Accessing the Web site in Java

??

First, a URL object is created from a path string as a parameter, and the openconnection method of the URL object is called to get a httpurlconnection References to Objects

??

Then call httpurlconnection 's Connect method to establish a connection, and after the connection is established, you can call httpurlconnection 's The Getheaderfields method gets a map object that stores the key-value pairs in the Header .

??

In addition to returning the Header , the server returns the response content, reads the response with a BufferedReader , and, of course, uses InputStreamReader as the parameter, and the input source of the InputStreamReader is the getinputstream method that calls HttpURLConnection

??

public static void Main (string[] args) throws IOException {

String Path = "http://www.baidu.com";

URL url = new URL (path);

HttpURLConnection httpurlconnection = (httpurlconnection) url.openconnection ();

Httpurlconnection.connect ();

map<string, list<string>> headermap = Httpurlconnection.getheaderfields ();

For (String Key:headerMap.keySet ()) {

SYSTEM.OUT.PRINTLN (key + ":" + headermap.get (key));

}

BufferedReader br = new BufferedReader (New InputStreamReader (

Httpurlconnection.getinputstream (), "Utf-8"));

String String=null;

while ((String=br.readline ())!=null) {

System.out.println (string);

}

Httpurlconnection.disconnect ();

}

Fundamentals: 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.