Summary of the fourth session of Java Training

Source: Internet
Author: User

Default layout: FlowLayout

Button

Button.setlabel ("Cancel");//Set the text on the button

Button.setenabled (b);//Set whether you can click

String Actioncommand = Button.getactioncommand ();//Gets information about the control

System.out.println (Actioncommand);

lable

Label.settext ("1123");//Set text

Label.gettext ();//Get Text


TextField text Box

Field.setechochar (' * '); To reflect the input in the form of ' * '

Field.seteditable (FALSE); Set whether you can enter

Field.setenabled (false);//setting is operable



Three common layout managers

1. FlowLayout Streamline Layout

Frame.setlayout (New FlowLayout ());//default center alignment, left and right, up and down 5

Frame.setlayout (New FlowLayout (Flowlayout.left));//Set left alignment, left and right, up or down 5

Frame.setlayout (New FlowLayout (flowlayout.left,20,40));//Set left alignment, left and right, up or down 5

2. BorderLayout Border Layout

In East

Strinbuffer

StringBuilder

Network programming

Website programming

Ip

192.168.1.1

A

1~126.0.0.0

B

128~191.xxx.0.0

C

192~255.xxx.xxx.0

255.255.255.0

The TCP protocol is a connection-oriented, reliable protocol TCP (Transmission Control Protocol) that is specifically designed to provide reliable, end-to-end byte-stream communication on unreliable internet. It is a connection-oriented protocol. A TCP connection is a byte stream, not a message flow.

TCP Three-time handshake

The other side establishes a reliable connection. A TCP connection has to go through three "dialogs" to build up, and the process is very complex and simply describes the simple process of three conversations: Host A sends a connection request packet to Host B: "I want to send you data, OK?" "This is the first conversation; Host B sends a consent connection to host A and requires synchronization (synchronization is the two host one in the send, one in the receiving, coordination work) packet:" Can, when do you send? " "This is the second conversation; Host a then sends a packet to confirm that Host B's requirements are synchronized:" I'll send it now, you go on! " ", this is the third dialogue. Three times the purpose of the "conversation" is to synchronize the sending and receiving of packets, and after three "conversations", host a formally sends the data to Host B.


The UDP protocol is an unreliable, non-connection protocol UDP (User Datagram Protocol) UDP that provides a way for the application to send an encapsulated raw IP datagram, and does not need to establish a connection when it is sent. is an unreliable connection.

Socket

Package com.xinboedu. Www.Test1;

Import Java.io.DataInputStream;
Import Java.io.DataOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.net.ServerSocket;
Import Java.net.Socket;

public class TestServer
{
static Boolean isAlive = true;

/**
* @param args
*/
public static void Main (string[] args)
{
Try
{
ServerSocket ss = new ServerSocket (6666);
while (isAlive)
{
Socket socket = ss.accept ();
New Clienthread (socket). Start ();
}
} catch (IOException e)
{
TODO auto-generated Catch block
E.printstacktrace ();
}
}

}

Class Clienthread extends Thread
{
private socket socket;
Private DataOutputStream dos;
Private DataInputStream dis;

Public clienthread (socket socket)
{
This.socket = socket;
Try
{
DOS = new DataOutputStream (Socket.getoutputstream ());
dis = new DataInputStream (Socket.getinputstream ());
} catch (IOException e)
{
TODO auto-generated Catch block
E.printstacktrace ();
}
}

@Override
public void Run ()
{
Try
{
while (true)
{
String str = DIS.READUTF ();
System.out.println (str);
Dos.writeutf ("Server: Received!! ");
}
} catch (IOException e)
{
TODO auto-generated Catch block
E.printstacktrace ();
}finally
{
Try
{
Socket.close ();
} catch (IOException e)
{
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
}

Package com.xinboedu. Www.Test1;

Import Java.io.DataInputStream;
Import Java.io.DataOutputStream;
Import java.io.IOException;
Import Java.io.OutputStream;
Import Java.net.Socket;
Import java.net.UnknownHostException;
Import Java.util.Scanner;

public class TestClient
{
static String name = "A";

/**
* @param args
*/
public static void Main (string[] args)
{
DataOutputStream dos;
DataInputStream dis;
Try
{
Socket socket = new Socket ("127.0.0.1", 6666);
DOS = new DataOutputStream (Socket.getoutputstream ());
dis = new DataInputStream (Socket.getinputstream ());

New Outthread (socket). Start ();
while (true)
{
String readUTF = Dis.readutf ();
System.out.println (READUTF);
}

} catch (Unknownhostexception e)
{
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e)
{
TODO auto-generated Catch block
E.printstacktrace ();
}
}

}

Class Outthread extends Thread
{
Private DataOutputStream dos;
Private Scanner Scanner;
private socket socket;

@SuppressWarnings ("resource")
Public outthread (socket socket)
{
This.socket = socket;
Try
{
DOS = new DataOutputStream (Socket.getoutputstream ());
} catch (IOException e)
{
TODO auto-generated Catch block
E.printstacktrace ();
}
Scanner = new Scanner (system.in);
}

@Override
public void Run ()
{
Try
{
while (true)
{
String next = Scanner.next ();
if ("Over". Equalsignorecase (Next))
{
Break
}
Dos.writeutf (Testclient.name + ":" + next);
}
} catch (IOException e)
{
TODO auto-generated Catch block
E.printstacktrace ();
}finally
{
Try
{
Socket.close ();
} catch (IOException e)
{
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
}

Udp

Receiving Party:

Package com.xinboedu. Www.Test;

Import java.io.IOException;
Import Java.net.DatagramPacket;
Import Java.net.DatagramSocket;
Import java.net.SocketException;

public class Testreciver
{

public static void Main (string[] args)
{
Try
{
Datagramsocket ds = new Datagramsocket (3333);
byte[] buf = new byte[1024];
Datagrampacket DP = new Datagrampacket (buf, buf.length);
Ds.receive (DP);
String string = new String (Dp.getdata (), 0,dp.getlength ());
System.out.println (string);
} catch (SocketException e)
{
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e)
{
TODO auto-generated Catch block
E.printstacktrace ();
}
}

}

Sender:

Package com.xinboedu. Www.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;

public class Testsend
{

public static void Main (string[] args)
{
Try
{
Datagramsocket ds = new Datagramsocket ();
String str = "ASDFASDFASDFASDF";
byte[] bytes = Str.getbytes ();
Datagrampacket DP =
New Datagrampacket (Bytes,
Bytes.length,
Inetaddress.getbyname ("127.0.0.1"),
3333);
Ds.send (DP);
} catch (SocketException e)
{
E.printstacktrace ();
} catch (Unknownhostexception e)
{
E.printstacktrace ();
} catch (IOException e)
{
E.printstacktrace ();
}
}

}

Tcp

Socket

Reliable, long-connected

Http

Reliable, Short link

One request, end of response, connection broken

URL Uniform Resource Locator

Get post

Www.baidu.com

http://192.168.1.1:8080?ww=1&wq=2

Package com.xinboedu. Www.Test;

Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import java.net.MalformedURLException;
Import Java.net.URL;
Import java.net.URLConnection;

public class Testhttp
{

/**
* @param args
*/
public static void Main (string[] args)
{
String Path = "Http://localhost:8080/day17.zip";
String str = "E:\\day.zip";
InputStream is = null;
FileOutputStream fos = null;
Try
{
URL url = new URL (path);
URLConnection openconnection = Url.openconnection ();
Openconnection.setconnecttimeout (1000);//Set connection time-out
Openconnection.setreadtimeout (1000);//Set Read timeout time
is = Openconnection.getinputstream ();
FOS = new FileOutputStream (str);
Byte[] B = new byte[1024];
int len = 0;
while ( -1! = (len = is.read (b)))
{
Fos.write (b, 0, Len);
}
Fos.flush ();
} catch (Malformedurlexception e)
{
E.printstacktrace ();
} catch (IOException e)
{
E.printstacktrace ();
}finally
{
if (is = null)
{
Try
{
Is.close ();
} catch (IOException e)
{
TODO auto-generated Catch block
E.printstacktrace ();
}
}

if (fos! = null)
{
Try
{
Fos.close ();
} catch (IOException e)
{
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}

}

}


Summary of the fourth session of Java Training

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.