简体   繁体   English

我的客户端服务器套接字有问题,我编写了代码,但是当我将信息(作为客户端)输入控制台时仍然没有任何反应

[英]I have a problem with my client server socket, i programmed the code but still when i enter info(as client) into the console nothing happens

so the problem im facing is when i run the client and put input i press enter nothing happens, it continues do scroll down, the client must enter information and then sends it to server, server receives information and adds it to linkedlist which is initialized in server class, after that the server preforms CalculateShippingPrice() and sends back output to client containing the price所以我面临的问题是,当我运行客户端并输入输入时,我按回车什么也没发生,它继续向下滚动,客户端必须输入信息然后将其发送到服务器,服务器接收信息并将其添加到链表中服务器类,之后服务器执行CalculateShippingPrice()并将包含价格的输出发送回客户端

class client班级客户

        //class CLient
        import java.io.*; .
        import java.net.*;
        class TCPClient {
        public static void main(String argv[]) throws Exception
        {
        String name;
        String ItemWeight;
        String PickUpLocation;
        String DistnationLocation;

        String modifiedSentence;// price to be returned to client
        BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
        Socket clientSocket = new Socket("localhost" , 3333);
        DataOutputStream outToServer = new DataOutputStream( clientSocket.getOutputStream());
        BufferedReader inFromServer = new BufferedReader(new 
        InputStreamReader(clientSocket.getInputStream()));

        System.out.println("please enter your name,itemWeight,pick up location,Distnation 
        location:");

        name = inFromUser.readLine(); //reads name
        ItemWeight = inFromUser.readLine(); //reads ItemWeight 
        PickUpLocation = inFromUser.readLine(); //reads PickUpLocation 
        DistnationLocation = inFromUser.readLine(); //reads DistnationLocation 


        outToServer.writeBytes(name + '\n'); //writes to server
        outToServer.writeBytes(ItemWeight + '\n');//writes to server
        outToServer.writeBytes(PickUpLocation + '\n');//writes to server
        outToServer.writeBytes(DistnationLocation + '\n');//writes to server


        modifiedSentence = inFromServer.readLine();//price to get from server
        System.out.println("FROM SERVER: " + modifiedSentence);//prints price
        clientSocket.close();
        }
        } 

class Server类服务器

        import java.io.*;

        import java.net.*; 
        class TCPServer {
        public static void main(String argv[]) throws Exception
        {
        List<customer> Shipping = new LinkedList<customer>();//initializes linked list from from 
        type customer that has all attributes  
        String name;
        String ItemWeight;
        String PickUpLocation;
        String DistnationLocation;
        String ItemID;
        String clientSentence;
        String capitalizedSentence;
        ServerSocket welcomeSocket = new ServerSocket (3333);

        while(true) {
        Socket connectionSocket = welcomeSocket.accept();
        BufferedReader inFromClient = new BufferedReader(new 
        InputStreamReader(connectionSocket.getInputStream()));
        DataOutputStream outToClient = new DataOutputStream( connectionSocket.getOutputStream());
        clientSentence = inFromClient.readLine(); //reads input from client
        name = clientSentence;//input from client
        ItemWeight = clientSentence;//input from client
        PickUpLocation = clientSentence;//input from client
        DistnationLocation = clientSentence;//input from client


        Shipping.insert(new customer(name, ItemWeight , PickUpLocation 
        ,DistnationLocation));//inserts info from client to linkedList in server

        capitalizedSentence = 
        "your shipping price is :" + Shipping.CalculateShippingPrice(Shipping);// calculates price
        outToClient.writeBytes(capitalizedSentence);//shows output to client 
        }
        }
        }    

There appears to be a few issues with your code that could be solved by using a debugger.您的代码似乎存在一些可以使用调试器解决的问题。 One issue is that the client is gathering and sending four lines of input, while the server is only reading one line of input.一个问题是客户端正在收集和发送四行输入,而服务器只读取一行输入。 I would guess that you probably want to read all four lines in the server (and remove clientSentence)?我猜您可能想读取服务器中的所有四行(并删除 clientSentence)?

        name = inFromClient.readLine();
        ItemWeight = inFromClient.readLine()
        PickUpLocation = inFromClient.readLine()
        DistnationLocation = inFromClient.readLine()

Also, the response MUST write a new line, or else the buffer won't be flushed.此外,响应必须写一个新行,否则缓冲区不会被刷新。 This could be resolved by adding a new line in the server:这可以通过在服务器中添加一个新行来解决:

        outToClient.writeBytes(capitalizedSentence + '\n');

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

相关问题 该代码可以运行和编译,但是当我输入很大的数字时什么也不会发生。 (Euler项目,问题3) - This code runs and compiles but nothing happens when I enter a very large number. (Euler project, problem 3) 我想在代码中使用jersey客户端,但导入了软件包,但仍然显示错误 - I want to use jersey client in my code I have imported the packages still it shows error 当我按Enter键时,Java中的Server Client仅显示消息 - Server Client in Java only displays message when I press enter 如何修改此代码以允许我的客户端套接字程序将字符串发送到服务器? - How can I modify this code to allow my client socket program to send a string to the server? 如果我的客户端程序是Java而不是C#,为什么必须添加服务器SSL证书? - Why do I have to add the server SSL certificate if my client program is in Java and not when it is in C#? 当我在Webview应用程序中单击“浏览图像”时,没有任何反应 - Nothing happens when I click “Browse Image” on my webview app 当我运行jar文件时,没有任何反应 - When I run my jar file nothing happens 当我单击Web视图内的链接时,没有任何反应 - Nothing happens when I click a link inside my webview 单击“连接”时什么也没发生 - Nothing Happens When I Click Connect 为什么我坚持不会发生什么? - Why nothing happens when I persist?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM