TCP客户端 - 服务器程序的问题

时间:2013-12-06 10:09:50

标签: java sockets tcp

以下是我的服务器程序

import java.io.*;
import java.net.*;

class TwoWayServer
{
   public static void main(String argv[]) throws Exception
      {
         ServerSocket server_socket = new ServerSocket(1234);

         while(true)
         {
            Socket listen_socket = server_socket.accept();
            BufferedReader client_input = new BufferedReader(new InputStreamReader(listen_socket.getInputStream()));
            DataOutputStream dos = new DataOutputStream(listen_socket.getOutputStream());
            String client_str = client_input.readLine();
            System.out.println(client_str);
            String st=client_str.toUpperCase();
            dos.writeBytes(st);
         }
      }
}

我的客户端程序如下,

import java.io.*;
import java.net.*;

class TwoWayClient
{
 public static void main(String argv[]) throws Exception
 {
  BufferedReader user_input = new BufferedReader( new InputStreamReader(System.in));
  Socket socket = new Socket("localhost", 1234);
  DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
  BufferedReader server_response = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  String client_str = user_input.readLine();
  dos.writeBytes(client_str + '\n');
  String response = server_response.readLine();
  System.out.println("FROM SERVER: " + response);
  socket.close();
 }
}

这里,如果我从客户端提供一个字符串,那么服务器将返回字符串的大写形式。但问题是,一旦我提供了字符串,我就不会从服务器得到任何响应。 请帮助。

2 个答案:

答案 0 :(得分:3)

问题是在服务器中没有使用换行符将字节写入服务器尝试替换dos.writeBytes(st);

dos.writeBytes(st + "\n");

答案 1 :(得分:3)

您可以尝试对代码进行这些修改。

服务器:

import java.io.*;
import java.net.*;

class TwoWayServer
{
        public static void main(String argv[]) throws Exception
        {
                ServerSocket server_socket = new ServerSocket(1234);

                while(true)
                {
                        Socket listen_socket = server_socket.accept();
                        BufferedReader client_input = new BufferedReader(new InputStreamReader(listen_socket.getInputStream()));
                        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(listen_socket.getOutputStream()));
                        String client_str = client_input.readLine();
                        System.out.println(client_str);
                        String st=client_str.toUpperCase();
                        bw.write(st + '\n');
                        bw.flush();
                }
        }
}

客户端:

import java.io.*;
import java.net.*;

class TwoWayClient
{
        public static void main(String argv[]) throws Exception
        {
                BufferedReader user_input = new BufferedReader( new InputStreamReader(System.in));
                Socket socket = new Socket("localhost", 1234);
                BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                BufferedReader server_response = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String client_str = user_input.readLine();
                bw.write(client_str + '\n');
                bw.flush();
                String response = server_response.readLine();
                System.out.println("FROM SERVER: " + response);
                socket.close();
        }
}