一具初学者的问题,请指教?

qynum123 2003-02-28 12:53:28
我编写了一个网络通信的程序,一个服务器,一个是客户端程序,我想实现这样的功能,客户端连接到服务器,客户端通过用户输入信息发送给服务器,服务器再将原信息返回,客户端输出信息,然后又等待用户输入,现在是服务器端可以给客户端发信息,客户端也能接收,但客户端给服务器端发信息服务器端收不到,不知道为什么?有没有人愿意帮助我?谢谢!
服务器端程序:
import java.io.*;
import java.net.*;
public class ServerEx
{
public static void main(String args[])
{
ServerSocket svrSocket = null;
Socket clientSocket;
int nConnect = 0;
String str;
try
{
svrSocket = new ServerSocket(8000,5);
while(nConnect < 5)
{
clientSocket = svrSocket.accept();
ServiceClient(clientSocket);
nConnect ++;
}
}
catch(IOException ioe)
{
System.out.println("Error in ServerEx" + ioe);
}
}
public static void ServiceClient(Socket client) throws IOException
{
DataInputStream inbound = null;
DataOutputStream outbound = null;
try
{
inbound = new DataInputStream(client.getInputStream());
outbound = new DataOutputStream(client.getOutputStream());
String inputLine;
outbound.writeBytes("Welcome to connected the Server\n");
while((inputLine = inbound.readLine())!="exit")
{
System.out.println("The Server answered:" + inputLine);
outbound.writeBytes(inputLine);
}
}
finally
{
System.out.println("Cleaning up connections:"+client);
inbound.close();
outbound.close();
client.close();
}
}
}
客户端程序:
import java.io.*;
import java.net.*;
public class ClientEx
{
public static void main(String args[]) throws IOException
{
//Socket client = null;
String inputLine=null,rev = null;
BufferedReader buf = null;
InetAddress addr = InetAddress.getByName(null);
Socket client = new Socket(addr,8000);
DataInputStream in = new DataInputStream(client.getInputStream());
DataOutputStream out = new DataOutputStream(client.getOutputStream());
try
{
buf = new BufferedReader(new InputStreamReader(System.in));
inputLine = in.readLine();
System.out.print(inputLine + "\n");
inputLine = buf.readLine();
for(;;)
{
if(inputLine.equals("exit"))
break;
System.out.println("Your's input is " + inputLine );
out.writeBytes(inputLine);
rev = in.readLine();
System.out.println("The server's answer is " + rev);
inputLine = buf.readLine();
}
}
finally
{
in.close();
out.close();
buf.close();
client.close();
}
}
}
...全文
58 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
MagicJohn 2003-03-03
  • 打赏
  • 举报
回复
有可能有这种情况,如果你是内部网的话,可能是有问题的。
qynum123 2003-02-28
  • 打赏
  • 举报
回复
这两个程序都在本机运行测试,所以就用getByName(null)了!
storm999 2003-02-28
  • 打赏
  • 举报
回复
你的addr為空,它連那個服務器?
---->InetAddress addr = InetAddress.getByName(null)﹔

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧