Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-02-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report