Java Network Programming Summary 1

Source: Internet
Author: User

1. Basic TCP network programming mode
The first is the server. The server uses serversocket,

 

Import java.net .*;
Import java. Io .*;

Public class tcpserver {
Public static void main (string [] ARGs) throws exception {
Serversocket Ss = new serversocket (6666 );
While (true ){

Socket S = ss. Accept ();

Datainputstream Dis = new datainputstream (S. getinputstream ());

System. Out. println (DIS. readutf ());
Dis. Close ();
S. Close ();
}
}
}

 

Client

Import java.net .*;
Import java. Io .*;

Public class tcpclient {
Public static void main (string [] ARGs) throws exception {
Socket S = new socket ("127.0.0.1", 6666 );
Outputstream OS = S. getoutputstream ();
Dataoutputstream dos = new dataoutputstream (OS );
Thread. Sleep (30000 );
Dos. writeutf ("Hello server! ");
Dos. Flush ();
Dos. Close ();
S. Close ();
}
}

For the client, the output is sent to the server.

 

2Program, You can output the client's IP port to the client through the server.

Import java.net .*;
Import java. Io .*;

Public class testserver {
Public static void main (string ARGs []) {
Try {
Serversocket S = new serversocket (8888 );
While (true ){
Socket S1 = S. Accept ();
Outputstream OS = s1.getoutputstream ();
Dataoutputstream dos = new dataoutputstream (OS );
Dos. writeutf ("hello," + s1.getinetaddress () +
"Port #" + s1.getport () + "bye-bye! ");
Dos. Close ();
S1.close ();
}
} Catch (ioexception e ){
E. printstacktrace ();
System. Out. println ("program running error:" + E );
}
}
}

 

The client displays the information entered by the server.

Import java.net .*;
Import java. Io .*;

Public class testclient {
Public static void main (string ARGs []) {
Try {
Socket S1 = new socket ("127.0.0.1", 8888 );
Inputstream is = s1.getinputstream ();
Datainputstream Dis = new datainputstream (is );
System. Out. println (DIS. readutf ());
Dis. Close ();
S1.close ();
} Catch (connectexception connexc ){
Connexc. printstacktrace ();
System. Err. println ("server connection failed! ");
} Catch (ioexception e ){
E. printstacktrace ();
}
}
}

 

 

3. The following TCP message is sent by the customer and the server to each other.
Server:
Import java. Io .*;
Import java.net .*;
Public class testsockserver {
Public static void main (string [] ARGs ){
Inputstream in = NULL;
Outputstream out = NULL;
Try {
Serversocket Ss = new serversocket (5888 );
Socket socket = ss. Accept ();
In = socket. getinputstream ();
Out = socket. getoutputstream ();
Dataoutputstream dos = new dataoutputstream (out );
Datainputstream Dis = new datainputstream (in );
String S = NULL;
If (S = dis. readutf ())! = NULL ){
System. Out. println (s );
System. Out. println ("from:" + socket. getinetaddress ());
System. Out. println ("port:" + socket. getport ());
}
Dos. writeutf ("hi, hello ");
Dis. Close ();
Dos. Close ();
Socket. Close ();
} Catch (ioexception e) {e. printstacktrace ();}
}
}

Client:
Import java.net .*;
Import java. Io .*;
Public class testsockclient {
Public static void main (string [] ARGs ){
Inputstream is = NULL; outputstream OS = NULL;
Try {
Socket socket = new socket ("localhost", 5888 );
Is = socket. getinputstream ();
OS = socket. getoutputstream ();
Datainputstream Dis = new datainputstream (is );
Dataoutputstream dos = new dataoutputstream (OS );
Dos. writeutf ("hey ");
String S = NULL;
If (S = dis. readutf ())! = NULL );
System. Out. println (s );
Dos. Close ();
Dis. Close ();
Socket. Close ();
} Catch (unknownhostexception e ){
E. printstacktrace ();
} Catch (ioexception e) {e. printstacktrace ();}
}
}

5 In UDP, pay attention to the application of mongorampacket. The following is the message sent from the client to the server.
Public class testudpclient
{
Public static void main (string ARGs []) throws exception
{
Byte [] Buf = (new string ("hello"). getbytes ();

Datagrampacket dp = new datagrampacket (BUF, Buf. length,
New inetsocketaddress ("Wagner. 0.0.1", 5678)
);
Datagramsocket DS = new datagramsocket (9999 );
DS. Send (DP );
DS. Close ();
}

Server:

Import java.net .*;

Public class testudpserver
{
Public static void main (string ARGs []) throws exception
{
Byte Buf [] = new byte [1024];
Datagrampacket dp = new datagrampacket (BUF, Buf. Length );
Datagramsocket DS = new datagramsocket (5678 );
While (true)
{
DS. Receive (DP );
System. Out. println (new string (BUF, 0, DP. getlength ()));
}
}
}

Note that if it is other types of data on the client, you should pay attention to the conversion. For example, the following example transfers a long integer to the server:
Client:
Public class testudpclient
{
Public static void main (string ARGs []) throws exception
{
Long n = 10000l;
Bytearrayoutputstream baos = new bytearrayoutputstream ();
Dataoutputstream dos = new dataoutputstream (baos );
Dos. writelong (N );

Byte [] Buf = baos. tobytearray ();
System. Out. println (BUF. Length );

Datagrampacket dp = new datagrampacket (BUF, Buf. length,
New inetsocketaddress ("Wagner. 0.0.1", 5678)
);
Datagramsocket DS = new datagramsocket (9999 );
DS. Send (DP );
DS. Close ();

}
}

Server:

Import java.net .*;
Import java. Io .*;

Public class testudpserver
{
Public static void main (string ARGs []) throws exception
{
Byte Buf [] = new byte [1024];
Datagrampacket dp = new datagrampacket (BUF, Buf. Length );
Datagramsocket DS = new datagramsocket (5678 );
While (true)
{
DS. Receive (DP );
Bytearrayinputstream BAIS = new bytearrayinputstream (BUF );
Datainputstream Dis = new datainputstream (BAIS );
System. Out. println (DIS. readlong ());
}
}
}


Related Article

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.