Get the App
SLTechnology News&Howtos  ›  Servers  › 

12.6-full Stack Java Notes: Java Network programming (4)

Shulou Source: shulou.com Published: 2022-06-02 17:22:08 09月22日 Update

Review in the previous section: after learning Socket to establish two-way communication between client and server, we found that its question and answer form was not flexible enough.

In this lesson, we will explain how to implement a chat room between the server and the client.

Questions to think about:

Server side: one thread sends messages and one thread receives messages.

Client: one thread sends messages and one thread receives messages.

In addition to digesting this code, you also need to think about how to implement a chat room! It is more difficult! )

[example 1] chat room server side

Import java.io.BufferedReader

Import java.io.BufferedWriter

Import java.io.IOException

Import java.io.InputStreamReader

Import java.io.OutputStreamWriter

Import java.net.ServerSocket

Import java.net.Socket

Public class ChatServer {

Public static void main (String [] args) {

ServerSocket server=null

Socket socket=null

BufferedReader in=null

Try {

Server = new ServerSocket (8888)

Socket=server.accept ()

New ServerThread (socket) .start ()

/ / the mainthread is responsible for reading the information sent by the client.

In=new BufferedReader (new InputStreamReader (socket.getInputStream ()

While (true) {

String str=in.readLine ()

System.out.println ("client says:" + str)

}

} catch (IOException e) {

E.printStackTrace ()

} finally {

Try {

In.close ()

} catch (IOException e) {

E.printStackTrace ()

}

Try {

Socket.close ()

} catch (IOException e) {

E.printStackTrace ()

}

}

}

}

/ * *

* Thread that specifically sends messages to the client

* @ author Administrator

*

, /

Class ServerThread extends Thread {

Socket ss

BufferedWriter out

BufferedReader br

Public ServerThread (Socket ss) {

This.ss = ss

Try {

Out=new BufferedWriter (new OutputStreamWriter (ss.getOutputStream ()

Br = new BufferedReader (new InputStreamReader (System.in))

} catch (IOException e) {

E.printStackTrace ()

}

}

Public void run () {

Try {

While (true) {

String str2 = br.readLine ()

Out.write (str2+ "\ n")

Out.flush ()

}

} catch (IOException e) {

E.printStackTrace ()

} finally {

Try {

Out.close ()

} catch (IOException e) {

E.printStackTrace ()

}

Try {

Br.close ()

} catch (IOException e) {

E.printStackTrace ()

}

}

}

}

[example 2] chat room client

Import java.io.BufferedReader

Import java.io.BufferedWriter

Import java.io.IOException

Import java.io.InputStreamReader

Import java.io.OutputStreamWriter

Import java.net.InetAddress

Import java.net.Socket

Import java.net.UnknownHostException

Public class ChatClient {

Public static void main (String [] args) {

Socket socket = null

BufferedReader in = null

Try {

Socket=new Socket (InetAddress.getByName ("192.168.1.32"), 8888)

New ClientThread (socket) .start ()

In=new BufferedReader (new InputStreamReader (socket.getInputStream ()

/ / the mainthread is responsible for receiving information from the server.

While (true) {

System.out.println ("Server says:" + in.readLine ())

}

} catch (UnknownHostException e) {

E.printStackTrace ()

} catch (IOException e) {

E.printStackTrace ()

} finally {

Try {

Socket.close ()

} catch (IOException e) {

E.printStackTrace ()

}

Try {

In.close ()

} catch (IOException e) {

E.printStackTrace ()

}

}

}

}

/ * *

* used to send messages to the server

* @ author Administrator

*

, /

Class ClientThread extends Thread {

Socket s

BufferedWriter out

BufferedReader wt

Public ClientThread (Socket s) {

This.s = s

Try {

Out=new BufferedWriter (new OutputStreamWriter (s.getOutputStream ()

Wt=new BufferedReader (new InputStreamReader (System.in))

} catch (IOException e) {

E.printStackTrace ()

}

}

Public void run () {

Try {

While (true) {

String str = wt.readLine ()

Out.write (str+ "\ n")

Out.flush ()

}

} catch (IOException e) {

E.printStackTrace ()

} finally {

Try {

Wt.close ()

} catch (IOException e) {

E.printStackTrace ()

}

Try {

Out.close ()

} catch (IOException e) {

E.printStackTrace ()

}

}

}

}

"full Stack Java Notes" is a series of Java engineer notes that can help you grow from zero to one. The author, known as Mr. G, has 10 years of experience in Java research and development. He has been engaged in software design and development in a research and development center of China Digital and Aerospace Academy, and has gradually become an engineer, senior engineer and architect. Proficient in Java platform software development, proficient in JAVAEE, familiar with various popular development frameworks.

The notes include six parts from shallow to deep:

Getting started with A-Java

B-database from entry to proficiency

C-hand blade moving front end and Web front end

D-J2EE from knowledge to actual combat

E-Java high-level framework refinement

F-Linux and Hadoop

Tags: Clients clients servers threads services messages chat rooms notes engineers advanced information front-end engineering frameworks examples software development large not enough between Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Shulou Technology vpn Redmi Microsoft