Create the following file Create
basic01Server.java
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class basic01Server {
public static void main(String[] args) {
try{
ServerSocket server = new ServerSocket(3838, 5);
while(true){
System.out.println("The server is running.");
Socket socket = server.accept();
PrintWriter output = new PrintWriter(socket.getOutputStream());
output.println("Hello! This is a server!");
output.close(); //PrintWriter is close()Basically close with
socket.close(); //Socket is close()Basically close with
}
} catch (Exception e){
System.out.println(e);
}
}
}
Compile and start.
Create the following files
basic01Client.java
import java.net.Socket;
import java.util.Scanner;
public class basic01Client {
public static void main(String[] args) {
try{
Socket socket = new Socket("127.0.0.1", 3838); //Use localhost server
Scanner input = new Scanner(socket.getInputStream());
System.out.println("The message from the server is "" + input.nextLine() + "」");
input.close(); //Scanner close()Basically close with
socket.close(); //Socket is close()Basically close with
} catch (Exception e){
System.out.println(e);
}
}
}
Let's run
basic01Server02.java
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner; //add to
public class basic01Server02 {
public static void main(String[] args) {
try{
ServerSocket server = new ServerSocket(3838, 5);
while(true){
System.out.println("The server is running.");
Socket socket = server.accept();
Scanner input = new Scanner(socket.getInputStream()); //add to:Stores the string received from the client
System.out.println("The message sent from the client is "" + input.nextLine() + ""is"); //add to:Output the stored character string
PrintWriter output = new PrintWriter(socket.getOutputStream());
output.println("Hello! This is a server!");
output.close();
socket.close();
}
} catch (Exception e){
System.out.println(e);
}
}
}
I haven't done it yet
basic01Client02.java
import java.net.Socket;
import java.util.Scanner;
import java.io.PrintWriter; // add
import java.io.*; // add
public class basic01Client02 {
public static void main(String[] args) {
try{
Socket socket = new Socket("127.0.0.1", 3838);
PrintWriter output = new PrintWriter(socket.getOutputStream()); // add
output.println("Send string from client"); // add
output.flush();
Scanner input = new Scanner(socket.getInputStream());
System.out.println("The message from the server is "" + input.nextLine() + "」");
input.close();
output.close(); // add
socket.close();
} catch (Exception e){
System.out.println(e);
}
}
}
Let's run
Rewrite the file on the client side.
basic02Client.java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class basic02Client {
public static void main(String args[]){
try{
Socket socket = new Socket("127.0.0.1", 3838);
PrintWriter output = new PrintWriter(socket.getOutputStream());
BufferedReader consoleIn = new BufferedReader( new InputStreamReader(System.in) ); // add
String consoleInStr = consoleIn.readLine(); // add
//output.println("----"); // remove
output.println(consoleInStr); // add
output.flush();
Scanner input = new Scanner(socket.getInputStream());
System.out.println(input.nextLine());
input.close();
output.close();
socket.close();
} catch(Exception e){
System.out.println(e);
}
}
}
Reference: Enter a string from the Java keyboard
basic03Server.java
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class basic03Server {
public static void main(String[] args) {
try{
ServerSocket server = new ServerSocket(3838, 5);
while(true){
System.out.println("The server is running.");
Socket socket = server.accept();
Scanner input = new Scanner(socket.getInputStream());
System.out.println("The message sent from the client is "" + input.nextLine() + ""is");
String clientIpAddress = socket.getRemoteSocketAddress().toString(); // add:1
System.out.println( "The IP address and port number of the client is "" + clientIpAddress + "」"); // add
String clientIpAddr = socket.getInetAddress().getHostAddress().toString(); // add:2
System.out.println( "The IP address and port number of the client is "" + clientIpAddr + "」"); // add
PrintWriter output = new PrintWriter(socket.getOutputStream());
output.println("Hello! This is a server!");
output.close();
socket.close();
}
} catch (Exception e){
System.out.println(e);
}
}
}
Reference: How to find the IP Address of Client connected to Server? --Stack overflow
basic04Server.java
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.util.Date;
public class basic04Server {
public static void main(String[] args) {
try{
ServerSocket server = new ServerSocket(3838, 5);
while(true){
System.out.println("The server is running.");
Socket socket = server.accept();
Scanner input = new Scanner(socket.getInputStream());
System.out.println("The message sent from the client is "" + input.nextLine() + ""is");
String clientIpAddress = socket.getRemoteSocketAddress().toString();
System.out.println( "The IP address and port number of the client is "" + clientIpAddress + "」");
String clientIpAddr = socket.getInetAddress().getHostAddress().toString();
System.out.println( "The IP address and port number of the client is "" + clientIpAddr + "」");
Date time = new Date(); // add
System.out.println( "Times of Day:"" + time + "」"); // add
PrintWriter output = new PrintWriter(socket.getOutputStream());
output.println("Hello! This is a server!");
output.close();
socket.close();
}
} catch (Exception e){
System.out.println(e);
}
}
}
basic05Server.java
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.util.Date;
public class basic05Server {
public static void main(String[] args) {
try{
ServerSocket server = new ServerSocket(3838, 5);
int order = 0; // add
while(true){
System.out.println("The server is running.");
Socket socket = server.accept();
order += 1; // add
Scanner input = new Scanner(socket.getInputStream());
System.out.println("The message sent from the client is "" + input.nextLine() + ""is");
String clientIpAddress = socket.getRemoteSocketAddress().toString();
System.out.println( "The IP address and port number of the client is "" + clientIpAddress + "」");
String clientIpAddr = socket.getInetAddress().getHostAddress().toString();
System.out.println( "The IP address and port number of the client is "" + clientIpAddr + "」");
Date time = new Date();
System.out.println( "Times of Day:"" + time + "」");
System.out.println( "This client is "" + order + "It is the third.");
PrintWriter output = new PrintWriter(socket.getOutputStream());
output.println("Hello! This is a server!");
output.close();
socket.close();
}
} catch (Exception e){
System.out.println(e);
}
}
}
basic06Server.java
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.util.Date;
public class basic06Server {
public static void main(String[] args) {
try{
ServerSocket server = new ServerSocket(3838, 5);
int order = 0; // add
while(true){
System.out.println("The server is running.");
Socket socket = server.accept();
order += 1; // add
Scanner input = new Scanner(socket.getInputStream());
System.out.println("The message sent from the client is ""); // change
String getString = input.nextLine();
while(!getString.equals("QUIT")){ // change:"QUIT"End when the character string of
System.out.println(getString); // change
getString = input.nextLine(); // add
}
System.out.println(""is"); // change
String clientIpAddress = socket.getRemoteSocketAddress().toString();
System.out.println( "The IP address and port number of the client is "" + clientIpAddress + "」");
String clientIpAddr = socket.getInetAddress().getHostAddress().toString();
System.out.println( "The IP address and port number of the client is "" + clientIpAddr + "」");
Date time = new Date();
System.out.println( "Times of Day:"" + time + "」");
System.out.println( "This client is "" + order + "It is the third.");
PrintWriter output = new PrintWriter(socket.getOutputStream());
output.println("Hello! This is a server!");
output.close();
socket.close();
}
} catch (Exception e){
System.out.println(e);
}
}
}
basic06Client.java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class basic06Client {
public static void main(String args[]){
try{
Socket socket = new Socket("127.0.0.1", 3838);
PrintWriter output = new PrintWriter(socket.getOutputStream());
BufferedReader consoleIn = new BufferedReader( new InputStreamReader(System.in) );
//String consoleInStr = consoleIn.readLine(); // remove
String consoleInStr = ""; // add
while(!consoleInStr.equals("QUIT")){ // change:"QUIT"Enter to finish
consoleInStr = consoleIn.readLine();
output.println(consoleInStr);
output.flush();
}
Scanner input = new Scanner(socket.getInputStream());
System.out.println(input.nextLine());
input.close();
output.close();
socket.close();
} catch(Exception e){
System.out.println(e);
}
}
}
Reference: Comparison of character strings with character strings
basic07Server.java
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.util.Date;
public class basic07Server {
public static void main(String[] args) {
try{
ServerSocket server = new ServerSocket(3838, 5);
int order = 0; // add
while(true){
System.out.println("The server is running.");
Socket socket = server.accept();
order += 1; // add
Scanner input = new Scanner(socket.getInputStream());
System.out.println("The message sent from the client is "");
String getString = input.nextLine();
while(!getString.equals("QUIT")){
System.out.println(getString);
getString = input.nextLine();
}
System.out.println(""is");
String clientIpAddress = socket.getRemoteSocketAddress().toString();
System.out.println( "The IP address and port number of the client is "" + clientIpAddress + "」");
String clientIpAddr = socket.getInetAddress().getHostAddress().toString();
System.out.println( "The IP address and port number of the client is "" + clientIpAddr + "」");
Date time = new Date();
System.out.println( "Times of Day:"" + time + "」");
System.out.println( "This client is "" + order + "It is the third.");
PrintWriter output = new PrintWriter(socket.getOutputStream());
//Multiple messages to return to the client
output.println( "START SERVER MESSAGE"); // add
output.println( "-------------------------------"); // add
output.println( "Hello!" ); // add
output.println( "This is a server." ); // add
output.println( "Sends a multi-line string."); // add
output.println( "-------------------------------"); // add
output.println( "END SERVER MESSAGE"); // add
//output.println("Hello! This is a server!"); // remove
output.close();
socket.close();
}
} catch (Exception e){
System.out.println(e);
}
}
}
basic07Client.java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class basic07Client {
public static void main(String args[]){
try{
Socket socket = new Socket("127.0.0.1", 3838);
PrintWriter output = new PrintWriter(socket.getOutputStream());
BufferedReader consoleIn = new BufferedReader( new InputStreamReader(System.in) );
String consoleInStr = "";
while(!consoleInStr.equals("QUIT")){
consoleInStr = consoleIn.readLine();
output.println(consoleInStr);
output.flush();
}
Scanner input = new Scanner(socket.getInputStream());
String serverMessage = ""; // add
while(!serverMessage.equals("END SERVER MESSAGE")){ // add
serverMessage = input.nextLine(); // add
System.out.println(serverMessage); // add
} // add
input.close();
output.close();
socket.close();
} catch(Exception e){
System.out.println(e);
}
}
}
Recommended Posts