Sockets Java套接字编程问题
哎,, 我正在尝试运行这个套接字编程代码 这是服务器端的代码-Sockets Java套接字编程问题,sockets,Sockets,哎,, 我正在尝试运行这个套接字编程代码 这是服务器端的代码- package sockettest; import java.net.*; import java.io.*; public class Server { public static void main(String[] args) throws IOException { ServerSocket serverSocket = null; try { serverSocke
package sockettest;
import java.net.*;
import java.io.*;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
try
{
serverSocket = new ServerSocket(139);
}
catch (IOException e)
{
System.err.println("not able to listen on port");
System.exit(1);
}
Socket clientSocket = null;
try
{
clientSocket = serverSocket.accept();
}
catch (IOException e)
{
System.err.println("Accept failed.");
System.exit(1);
}
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); // Out is Outputstream is used to write to the Client .
BufferedReader in = new BufferedReader( new InputStreamReader(clientSocket.getInputStream())); // in is used to read the Client's input.
String inputLine, outputLine;
out.println("Hey! . Who are you?"); // Writes to client as "Hey! . Who are you?"
while ((inputLine = in.readLine()) != null)
{
// Reads the input from the Client. if it is "bye" the program ends.
if (inputLine.equalsIgnoreCase("Bye"))
{
out.println("Bye");
break;
}
else
{
out.println("Hello Mr. " + inputLine);
}
}
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
}
这是在客户端运行的代码-
import java.io.*;
import java.net.*;
public class Client
{
public static void main(String[] args) throws IOException {
Socket kkSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try
{
kkSocket = new Socket("192.168.2.3", 139);
out = new PrintWriter(kkSocket.getOutputStream(), true); // Out may be used to write to server from the client
in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream())); // in will be used to read the lines sent by the Server.
}
catch (UnknownHostException e)
{
System.err.println("Unidentified host.");
System.exit(1);
}
catch (IOException e)
{
System.err.println("Couldn't get I/O for the connection to.");
System.exit(1);
}
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String fromServer;
String fromClient;
while ((fromServer = in.readLine()) != null) {
System.out.println("Server: " + fromServer);
if (fromServer.equals("Bye"))
break;
fromClient = stdIn.readLine();
if (fromClient != null) {
System.out.println("Client: " + fromClient);
out.println(fromClient);
}
}
out.close();
in.close();
stdIn.close();
kkSocket.close();
}
}
我正在客户端和服务器端运行eclipse上的代码。使用cmd提示符中的netstat命令,我可以看到客户端和服务器之间已经建立了连接,但是我无法通信,eclipse也没有显示任何输出。怎么了 你还没有告诉我们问题出在哪里。但是,粗略地看一下您的代码,我建议不要监听端口139,因为它已经被Windows下的NetBios使用,可能会导致冲突。您的服务器代码也丢失了 inputLine的初始化, e、 g 字符串inputline=“”
在执行while循环之前请记住,如果您读取或写入 您的客户端一直在读取,因为它等待服务器上的所有信息 直到它为空 您的服务器也一直在读取数据,并等待任何输入 所以,只要服务器和客户端都在等待输入,就不会有人收到任何数据 试着想出一个在服务器和客户端之间通信的协议。 e、 g
发送消息时也需要out.flush()。。。你的问题是什么?嗨,到底是什么问题?代码不编译?你不知道它是怎么工作的?它目前的结构只允许一个客户端连接。对不起。我已对其进行了编辑,并在最后添加了我的问题。请阅读并相应回复。谢谢你的代码没有缩进,读起来很混乱。让读者有礼貌地阅读格式良好的代码。还有,你所说的“无法沟通”到底是什么意思?不要给出模糊的描述。目标要精确。没有向服务器发送任何内容?服务器没有接收任何内容?您是否尝试在eclipse调试器上运行服务器,并且在循环时服务器的输入上有一个断点?@luis.espinal:没有向服务器或客户端发送任何内容。正如您所说,我将尝试在eclipse调试器上运行它。在UNIX上,您需要是root用户才能打开<1024个端口。很抱歉。我已对其进行了编辑,并在最后添加了我的问题。请阅读并相应回复。谢谢你能告诉我哪些端口是免费的吗?@BenoitThiery:谢谢你的回复,但我正在Windows Vista上运行它。你不需要它。OP代码执行一些在Java中通常被认为是不好的操作。事实上,感觉OP是以一种非常类似C的方式编码的。话虽如此,用“”初始化字符串变量,特别是当它立即用其他东西初始化时,这也是徒劳的。这是Java,不是C。这两种语言有非常不同的编码标准和最佳实践。一个人不能简单地从一个人身上拿走,然后把它强加到另一个人身上。我很抱歉。我已对其进行了编辑,并在最后添加了我的问题。请阅读并相应回复。谢谢字符串inputline已初始化。“字符串inputLine,outputLine;”在缓冲区中的行后面=。。。。这还不够吗?@Benoit-你会惊讶于我发现有这么多的Java程序员在工作(用“”初始化字符串变量,认为这是实际需要的。)同时也很悲哀和有趣。客户端和服务器都在尝试通信,但没有向任何一方发送任何消息。我应该在哪里添加out.flush()?我刚刚试过你的代码,除了端口,一切都很好,我把它改为33139,把主机改为localhost,一切都正常
Sever to Client: Hello Who are you?
Client receives Data and replies: Client
Server receives Information: You Are now authorized, what ya gonna do?
and so on ^^