TCP Implementations in Java

Source: Internet
Author: User

TCP Implementations in Java

The code is from the computer network textbook

This is the server-side code

  
  1. package server;
  2. import java.io.BufferedReader;
  3. import java.io.DataOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.net.ServerSocket;
  7. import java.net.Socket;
  8. public class TcpServer {
  9. public static void main(String[] args) throws IOException {
  10. String getString;
  11. String sendString;
  12. ServerSocket socket = new ServerSocket(6789);
  13. //ServerSocket 为服务器监听的类
  14. while(true) {
  15. //收到accept请求,返回socket实例
  16. Socket connection = socket.accept();
  17. System.out.println("connect successfully");
  18. //获得输入的内容,即客户端程序发送过来的消息
  19. BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  20. //发送回去的内容
  21. DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
  22. getString = reader.readLine();
  23. sendString = getString.toUpperCase() + "\n";
  24. //将内容写入到想要返回给客户端的流中
  25. outputStream.writeBytes(sendString);
  26. }
  27. }}

Client class
Package client;

  
  1. import java.io.BufferedReader;
  2. import java.io.DataOutputStream;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.net.Socket;
  6. import java.net.UnknownHostException;
  7. import java.nio.Buffer;
  8. public class TcpClient {
  9. public static void main(String[] args) throws UnknownHostException, IOException {
  10. // TODO Auto-generated method stub
  11. String sendString;
  12. String getString;
  13. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
  14. System.out.println("start connection");
  15. //开始连接,参数为host,如果为本地的话,则为127.0.0.1,端口跟服务器使用端口相同
  16. Socket socket = new Socket("45.62.102.41", 6789);
  17. System.out.println("connect successfully");
  18. //发送的内容
  19. DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
  20. //接收的内容
  21. BufferedReader fromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  22. sendString = bufferedReader.readLine();
  23. outputStream.writeBytes(sendString + "\n");
  24. getString = fromServer.readLine();
  25. System.out.print(getString);
  26. socket.close();
  27. }}

As you can see, this connection ensures that the data is transferred. Before we transmit the data, we create a new virtual channel (from socket to serversocket), and we can transfer the data through the data stream.



From for notes (Wiz)

TCP Implementations in Java

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.