简体   繁体   English

简单的客户端服务器程序不起作用

[英]Simple client server program not working

Server program : 服务器程序:

import java.io.*;
import java.net.*;
public class server
{
        public static void main(String args[])
        {
                try
                {
                ServerSocket ss=new ServerSocket(2000);
                Socket s=ss.accept();
                BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
                String str;
                while((str=br.readLine())!=null)
                {
                        System.out.println(str);
                }
                }
                catch(Exception e)
                {
                        System.out.println(e);
                }
        }
}

Client program : 客户程序:

import java.net.*;
import java.io.*;
public class client
{
        public static void main(String args[])
        {
                try
                {
                Socket s=new Socket("127.0.0.1",2000);
                String str;
                BufferedWriter br=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
                br.write("\nHello World\n");
                }
                catch(Exception e)
                {
                        System.out.println(e);
                }
        }
}

The issues that I am facing are: 我面临的问题是:

  1. No output. 无输出。
  2. No Exception/Error is indicated. 没有异常/错误指示。

Please explain me if am doing anything wrong. 如果做错任何事情,请向我解释。 The problem might be the client has not written anything while the server is reading. 问题可能是服务器正在读取时客户端没有写任何东西。

Please add some debug statement to check 请添加一些调试语句以进行检查

(1) is client able to make the connection with running server or not. (1)客户端是否可以与正在运行的服务器建立连接。 so in server part add 所以在服务器部分添加

Socket s=ss.accept();
System.out.println("one new connection");

(2) also in client program add flush() after the br.write line (2)同样在客户端程序中,在br.write行之后添加flush()

 br.write("\nHello World\n");
 br.flush()

 // use the below statement as well at last (if you no longer want to use the output stream)
 br.close();

Please note you are just write one time here.... for continuous reading and writing you will have to run this in loop.... OR to run multiple clients simultaneously ... you will have to execute each socket connection (after accepting it) into different thread at server end 请注意,您在这里只写一次。...要连续读写,您必须在循环中运行它。...或者要同时运行多个客户端...您将必须执行每个套接字连接(接受后它)到服务器端的不同线程中

Close the stream after writing to stream in client program br.close(); 在客户端程序br.close();写入流后,关闭流br.close();

After Writing to stream it is compulsory to close the stream or flush the stream( br.flush() ) because when stream is closed then only that stream can be read. 在写入流之后,必须关闭流或刷新流( br.flush() ),因为在关闭流时只能读取该流。 I/O operations can not be performed on same stream simultaneously. I / O操作不能同时在同一流上执行。

Two sockets are connected by same stream so I/O operations can not be performed simultaneously on that stream. 两个套接字通过同一流连接,因此无法在该流上同时执行I / O操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM