In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-13 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.