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

Java Socket Communication how to carry out the Information Communication of the client

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article shows you how Java Socket communication carries on the client-side information communication, the content is concise and easy to understand, absolutely can make your eyes bright, through the detailed introduction of this article, I hope you can get something.

Java Socket communication in the actual use of a lot of key code we need to learn, so first let's look at how to deliver mail between clients.

Import java.io.*

Public class Jserver2 {

Private BufferedReader reader; / / responsible for input

Private ServerSocket server; / / server socket

Private Socket socket; / / socket

Public Server2 () {} / / default constructor

Void startServer () / / start the server

{

Try

{

Server=new ServerSocket / / create server sockets

System.out.println ("Server socket established")

While (true)

{

System.out.println ("waiting for client GG")

Socket=server.accept (); / / if the client GG makes a connection request, connect with socket

System.out.println ("complete connection with client")

Reader=new BufferedReader (new InputStreamReader (socket.

GetInputStream (), "UTF-8"); / / get socket input stream, "utf-8"

This coding setting is for better display of Chinese.

GetMessage (); / / read the data from the client and output it to the screen

}

} catch (Exception e)

{

System.out.println (e)

} finally {

Try

{

If (server socket null) server.close (); / / closes the server socket.

} catch (IOException ie) {}

}

}

Void getMessage () / / read the information from the socket

{

Try

{

While (true) / / Loop

{

System.out.println ("client GG says:" + reader.readLine ())

}

} catch (Exception e) {}

Finally {

System.out.println ("client disconnect")

Try

{

If (readerless null) reader.close (); / / closes the input stream of the socket

If (socketpacking null) socket.close (); / / close the socket

Reader=null

Socket=null

} catch (Exception e) {}

}

}

Public static void main (String [] args)

{

Server2 server=new Server2 ()

Server.startServer ()

}

}

The above content is Java Socket communication how to carry out client-side information communication, have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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

Development

Wechat

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

12
Report