Java套接字客户端&断绝 [英] Java Socket client & sever

查看:83
本文介绍了Java套接字客户端&断绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试下面的代码发送文件和目录的列表,从服务器到客户端。服务器正在从客户端接收,但我不知道服务器是否没有发送回结果或客户端不接受它。请帮助我。

I tried the following code to send list of files and directories, from server to client. the server is receiving from client but i don't know whether server is not sending back the result or client is not accepting it. Please help me out.Thanks

服务器端: -

package without.thread;


import java.io.BufferedOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import static java.rmi.Naming.list;
import java.util.ArrayList;
import static java.util.Collections.list;
import java.util.logging.Level;
import java.util.logging.Logger;



public class SerTest {
  public static int reads,red;  
    public static void main(String[] args) 
    { 

          try
                  {
                      System.out.print("i m ready, call my client");
            ServerSocket serverSocket = new ServerSocket(18789);
                       while(true){ 
            Socket clientSocket = serverSocket.accept();
                        System.out.println(clientSocket+"1");
                       // outk=new PrintWriter(clientSocket.getOutputStream(),true); 

                         BufferedReader bufferedReader;
       bufferedReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));


                }


推荐答案

从我可以看到你在客户端上做 readLine(),而执行 outqw.write()服务器。在从服务器发送的字符串中没有行尾字符,因此客户端将永远不能完成 readLine 。执行outqw.println()或添加\\\
到发送的任何结尾。话虽这么说,很难导航未格式化的代码与一堆注释掉的东西,所以问题可能是别的。

From what I can see you do readLine() on the client, while doing outqw.write() on the server. There is no end of line character in the string sent from server, so the client will never be able to finish the readLine. Do the outqw.println() or add "\n" to the end of whatever you're sending. That being said, it's very hard to navigate unformatted code with bunch of commented out stuff so the problem might be something else.

这篇关于Java套接字客户端&断绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆