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

How to use socket

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

Share

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

This article is about how to use socket. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.

In use, the transmission format of the data used is agreed upon, etc. See Code

Socket socket = new Socket();//new a socket client

SocketAddress host = new InetSocketAddress(InetAddress.getLocalHost(),5678);

socket.connect(host);//to connect the host

socket.setSoTimeout(60000);//set to 60 seconds

PrintWriter out=new PrintWriter(socket.getOutputStream());//Get output stream, write request information

out.print(functionCode+len+cardInfo+');

out.flush();//must have

BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));//Get server return information

char[] returnCode = new char[3];//return code

char[] returnLen = new char[6];//Returns the length of the packet

StringBuffer sb = new StringBuffer();

in.read (returnCode,0,3);//read return code

sb.append(returnCode);

sb.setLength(0);//original data in case sb

in.read (returnLen,0,6);//Read packet length information

sb.append(returnLen);

out.close();

in.close();

socket.close();

Above, mainly related to write send request information, read return information, also used StringBuffer, you can set its length to 0, you can reuse it.

Above is the client code, below is the server code, omitting the socket connection.

BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream()));

//function number

char[] functionCode = new char[3];

//packet length

char[] returnLen = new char[6];

StringBuffer sb = new StringBuffer();

//remove function number

in.read(functionCode,0,3);

String funtion = sb.append(functionCode).toString();

sb.setLength(0);

System.out.println("function number: "+funtion);

in.read(returnLen,0,6);

int baglength = Integer.parseInt(sb.append(returnLen).toString());

sb.setLength(0);

System.out.println("package length: "+baglength);

char[] message = new char[baglength];

in.read(message);

System.out.println(message);

//sleep(10000);

PrintWriter out=new PrintWriter(client.getOutputStream());

out.print("100"+');

out.flush();

in.close();

out.close();

Thank you for reading! About "socket how to use" this article is shared here, I hope the above content can be of some help to everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!

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