Java Socket客户端&服务器 [英] Java Socket client & server

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

问题描述

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

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.

服务器端:

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()或添加\ n到您发送的任何内容的末尾。话虽如此,很难用一堆注释掉的东西来导航未格式化的代码,所以问题可能就是其他问题。

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 Socket客户端&服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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