[英]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.