[英]Java Socket programming in cmd
So I have the following code that runs on the telnet login in linux. 因此,我有以下代码在Linux的telnet登录上运行。 How to make this run in cmd?
如何使它在cmd中运行? I tried running the tcp client and server programs in cmd but I am not getting the output.
我尝试在cmd中运行tcp客户端和服务器程序,但没有得到输出。 Here is my code that runs on linux telnet but not on windows cmd.
这是我的代码,它在linux telnet上运行,但不在Windows cmd上运行。
import java.io.*;
import java.net.*;
class tcpclient{
public static void main(String args[])throws Exception{
String hwaddr,modsentence;
BufferedReader inFromUser=new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket=new Socket("localhost",2057);
DataOutputStream outToServer= new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer=new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
System.out.print("Enter Ip address");
hwaddr=inFromUser.readLine();
outToServer.writeBytes(hwaddr+""+"\n");
modsentence=inFromServer.readLine();
System.out.println("FROM SERVER:"+modsentence);
clientSocket.close();
}
}
import java.net.*;
import java.io.*;
class tcpserver{
public static void main(String args[])throws Exception
{
String clientSentence;
ServerSocket welcomeSocket = new ServerSocket(2057);
while(true)
{
Socket connectionSocket=welcomeSocket.accept();
BufferedReader inFromClient=new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient=new DataOutputStream(connectionSocket.getOutputStream());
clientSentence=inFromClient.readLine();
if(clientSentence.equals("a"))
outToClient.writeBytes("testtt");
}
}
}
To answer the question: It's not relevant whether you use bash or CMD. 要回答这个问题:使用bash还是CMD都无关紧要。 Socket programming is the same because the shell doesn't interfere.
套接字编程是相同的,因为外壳不会干扰。 You send some characters to the server and the servers sends some characters back.
您向服务器发送一些字符,服务器又向后发送一些字符。 I have tested this in CMD and it works the same.
我已经在CMD中对此进行了测试,并且工作原理相同。
Change 更改
outToClient.writeBytes("testtt");
to 至
outToClient.writeBytes("testtt\n");
According to https://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html#readLine() 根据https://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html#readLine()
readLine() Reads a line of text.
readLine()读取一行文本。 A line is considered to be terminated by any one of a line feed ('\\n') , a carriage return ('\\r'), or a carriage return followed immediately by a linefeed.
一行被认为由换行符('\\ n') ,回车符('\\ r')或回车符后紧跟换行符之一终止。
This would make 这将使
modsentence = inFromServer.readLine();
return a String and assign it to modsentence. 返回一个String并将其分配给modsentence。
Also remember that your tcpserver only responds to character "a". 还请记住,您的tcpserver仅响应字符“ a”。
If you use a BufferedReader then you should also use a BufferedWriter . 如果使用BufferedReader,则还应该使用BufferedWriter 。 Also note
DataInputStream.readLine()
is deprecated and should not be used. 还要注意,不建议使用
DataInputStream.readLine()
,不应使用它。 With all that being said, yes there is a difference between the Command Prompt in Windows, and the Shell in Linux. 话虽这么说,是的,Windows中的命令提示符和Linux中的Shell之间是有区别的。 Windows uses \\r\\n as a line terminator, and Linux uses \\n .
Windows使用\\ r \\ n作为行终止符,而Linux使用\\ n 。 If you use \\r\\n as a line feed, it should work fine on all platforms.
如果您将\\ r \\ n用作换行符,则在所有平台上都应能正常工作。 However you never will realistically use the Command Prompt in a real client/server architecture so it's best not to base your application off of windows ancient features.
但是,您永远不会在实际的客户端/服务器体系结构中实际使用命令提示符,因此最好不要将应用程序基于Windows的古老功能。 use \\n .
使用\\ n 。
Try using Scanner to get input from System.in 尝试使用Scanner从System.in获取输入
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.