Chapter One Introduction to Java Network programming _receiver_sender

Source: Internet
Author: User

Import java.io.*;
Import java.net.*;
public class Receiver {
private int port=8000;
Private ServerSocket ServerSocket;
private static int stopway=1; Ways to end communication
private final int natural_stop=1; Natural End
private final int sudden_stop=2; Abrupt termination of the program
private final int socket_stop=3; Close the socket, and then end the program
private final int input_stop=4; Close the input stream, and then end the program
private final int serversocket_stop=5; Close ServerSocket, and then end the program

Public Receiver () throws IOException {
ServerSocket = new ServerSocket (port);
SYSTEM.OUT.PRINTLN ("Server has been started");
}

Private BufferedReader Getreader (socket socket) throws ioexception{
InputStream socketin = Socket.getinputstream ();
return new BufferedReader (new InputStreamReader (Socketin));
}

public void receive () throws exception{
Socket Socket=null;
Socket = Serversocket.accept ();
BufferedReader BR =getreader (socket);

for (int i=0;i<20;i++) {
String Msg=br.readline ();
System.out.println ("Receive:" +msg);
Thread.Sleep (1000);
if (i==2) {//Terminate program, end communication
if (stopway==sudden_stop) {
SYSTEM.OUT.PRINTLN ("Abrupt termination procedure");
System.exit (0);
}else if (stopway==socket_stop) {
SYSTEM.OUT.PRINTLN ("Close socket and Terminate program");
Socket.close ();
Break
}else if (stopway==input_stop) {
System.out.println ("Close the input stream and terminate the program");
Socket.shutdowninput ();
Break
}else if (stopway==serversocket_stop) {
System.out.println ("Close the ServerSocket and terminate the program");
Serversocket.close ();
Break
}
}
}

if (stopway==natural_stop) {
Socket.close ();
Serversocket.close ();
}
}

public static void Main (String args[]) throws Exception {
if (args.length>0) Stopway=integer.parseint (Args[0]);
New Receiver (). receive ();
}
}

Sender.java

Import java.net.*;
Import java.io.*;
Import java.util.*;
public class Sender {
Private String host= "localhost";
private int port=8000;
private socket socket;
private static int stopway=1; Ways to end communication
private final int natural_stop=1; Natural End
private final int sudden_stop=2; Abrupt termination of the program
private final int socket_stop=3; Close the socket, and then end the program
private final int output_stop=4; Close the output stream, and then end the program

Public Sender () throws ioexception{
Socket=new Socket (Host,port);
}
public static void Main (String args[]) throws exception{
if (args.length>0) Stopway=integer.parseint (Args[0]);
New Sender (). Send ();
}
Private PrintWriter getwriter (socket socket) throws ioexception{
OutputStream socketout = Socket.getoutputstream ();
return new PrintWriter (socketout,true);
}


public void Send () throws Exception {
PrintWriter pw=getwriter (socket);
for (int i=0;i<20;i++) {
String msg= "Hello_" +i;
PW.PRINTLN (msg);
System.out.println ("Send:" +msg);
Thread.Sleep (500);
if (i==2) {//Terminate program, end communication
if (stopway==sudden_stop) {
SYSTEM.OUT.PRINTLN ("Abrupt termination procedure");
System.exit (0);
}else if (stopway==socket_stop) {
SYSTEM.OUT.PRINTLN ("Close socket and Terminate program");
Socket.close ();
Break
}else if (stopway==output_stop) {
Socket.shutdownoutput ();

System.out.println ("Turn off the output stream and terminate the program");
Break
}
}
}

if (stopway==natural_stop) {
Socket.close ();
}
}
}

Chapter One Introduction to Java Network programming _receiver_sender

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.