TCP transmission: Use the socket service to implement a text converter, socket Text Converter

Source: Internet
Author: User

TCP transmission: Use the socket service to implement a text converter, socket Text Converter

Reprinted please indicate the source, thank you: http://blog.csdn.net/harryweasley/article/details/45665291


I recently read a teaching video and learned socket programming. There is an example in which I feel that it is well written. I will organize it here to help me recall and view it.


Requirement: Text Converter,

The client sends some letters to the server. The server converts the text into uppercase and returns it to the client.

In addition, the client can perform unlimited text conversion. When the client inputs "over", the conversion ends.


:

Client:


Server:



About dos window, run java, you can read this article http://blog.csdn.net/harryweasley/article/details/45559129

Analysis:
Client:
Since the data on the device is operated, I/O technology can be used and considered according to the operating rules of I/O.
Source: keyboard input.
Objective: network device, network output stream.
In addition, the operation is text data. You can select a streaming.


Procedure
1. Create a service.
2. Obtain keyboard input.
3. send data to the server.
4. Obtain the upper-case data returned by the server.
5. Close the resource.


All are text data. You can use the response stream to perform operations, increase efficiency, and add buffering.


The following is the client code:

Import java. io. bufferedReader; import java. io. IOException; import java. io. inputStreamReader; import java. io. printWriter; import java.net. socket; import java.net. unknownHostException; public class TransClien {/*** @ param args * @ throws IOException * @ throws UnknownHostException */public static void main (String [] args) throws Exception {// create a client socket and specify the destination host and port Socket s = new Socket ("192.168.1.48", 10005 ); // Define the stream object for reading the keyboard data. BufferedReader bufr = new BufferedReader (new InputStreamReader (System. in); // defines the purpose of writing data to the socket output stream. Send to the server. // BufferedWriter bufOut = // new BufferedWriter (new OutputStreamWriter (s. getOutputStream (); PrintWriter out = new PrintWriter (s. getOutputStream (), true); // defines a socket to read the stream and read the upper-case information returned by the server. BufferedReader bufIn = new BufferedReader (new InputStreamReader (s. getInputStream (); String line = null; while (line = bufr. readLine ())! = Null) {if ("over ". equals (line) break; out. println (line); // bufOut. write (line); // bufOut. newLine (); // bufOut. flush (); String str = bufIn. readLine (); System. out. println ("server:" + str);} bufr. close (); s. close ();}}


Server:
Source: socket reads the stream.
Purpose: socket output stream.
All are text and decoration.


The following is the server code:

Import java. io. bufferedReader; import java. io. inputStreamReader; import java. io. printWriter; import java.net. serverSocket; import java.net. socket; public class TransServer {/*** @ param args */public static void main (String [] args) throws Exception {// create a socket service, the listening port ServerSocket ss = new ServerSocket (10005); Socket s = ss. accept (); String ip = s. getInetAddress (). getHostAddress (); System. out. println (ip +" ... Connected "); // reads data from the stream from the socket. BufferedReader bufIn = new BufferedReader (new InputStreamReader (s. getInputStream (); // purpose. Socket output stream. Write uppercase data to the socket output stream and send it to the client. // BufferedWriter bufOut = // new BufferedWriter (new OutputStreamWriter (s. getOutputStream (); PrintWriter out = new PrintWriter (s. getOutputStream (), true); String line = null; while (line = bufIn. readLine ())! = Null) {System. out. println (line); out. println (line. toUpperCase (); // bufOut. write (line. toUpperCase (); // bufOut. newLine (); // bufOut. flush ();} s. close (); ss. close ();}}

Note: The four lines of code commented on by the client and the server are made using the buffer stream, and the PrintWriter step is less. Both methods are acceptable.


For udp transmission, you can view this article http://blog.csdn.net/harryweasley/article/details/45665309


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.