Java network programming learning Java chat program code sharing
- 2020-04-01 02:39:51
- OfStack
package com.neusoft.edu.socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class ChatServer {
public static void main(String[] args) {
try {
//1, create the ServerSocket object,8875 for the custom port number
ServerSocket server = new ServerSocket(8857);
//Simple tips
System.out.println(" Waiting for client connection... ");
//2. Get client connection
Socket client = server.accept();
//Get information about the client
System.out.println(client.getInetAddress().getHostAddress() + " It's connected... ");
//3.1 define the input stream and output stream objects
BufferedReader in = new BufferedReader(
new InputStreamReader(
client.getInputStream()));
//To get the data entered from the console and send it to the client
BufferedReader inByServer = new BufferedReader(
new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(client.getOutputStream(), true);
//Read the data
String data = null;
String answer = null;
//The loop communicates with the client
do
{
//3.2 read the data sent by the client
data = in.readLine();
// Displays on the server side Read the data
System.out.println(" Client sends message: " + data);
//Gets the information the server wants to send to the client
System.out.print(" Server-side reply client: ");
answer = inByServer.readLine();
//3.3. Write data to the client
out.println(answer);
out.flush();
}while(!"bye".equals(data));
//4. Close related resources
out.flush();
in.close();
inByServer.close();
out.close();
//Close Socket object
client.close();
server.close();
System.out.println(" Server side down... ");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.neusoft.edu.socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class ChatClient {
public static void main(String[] args) {
try {
//1, create Socket objects, ("192.168.1.107", 8857) is the host IP and port number, the two class port number to be consistent
Socket client = new Socket("192.168.1.107", 8857);
//2.1 create input and output stream objects
BufferedReader in = new BufferedReader(
new InputStreamReader(
client.getInputStream()));
PrintWriter out = new PrintWriter(client.getOutputStream(), true);
BufferedReader inByClient = new BufferedReader(
new InputStreamReader(System.in));
//Data sent from the server side
String data = null;
//Data written by the client to the server
String answer = null;
do
{
//2.2 read and write operations of the client
System.out.print(" You say: ");
//Gets the data to be sent to the server side
answer = inByClient.readLine();
//Writes data to the server side
out.println(answer);
out.flush();
// To obtain Data sent from the server side
data = in.readLine();
//Output the data retrieved from the server side
System.out.println(" The information returned from the server side is: " + data);
}while(!"bye".equals(data));
//3, shut down
in.close();
out.close();
inByClient.close();
client.close();
System.out.println(" Client closed... ");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}