急急急 java 网络题目,求大神解答
我这两个代码,能够收到你好,但是“恩,你也好”收不到,收到的是地址,和端口,这是什么情况?publicclassTcpClient{publicstaticvoidmai...
我这两个代码,能够收到你好,但是“恩,你也好”收不到,收到的是地址,和端口,这是什么情况?
public class TcpClient
{
public static void main(String[] args)
{
try {
Socket s = new Socket("localhost",6970);
InputStream is = s.getInputStream();
OutputStream os = s.getOutputStream();
DataInputStream dis = new DataInputStream(is);
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF("你好");
String ss = null;
if((ss=dis.readUTF())!=null);
System.out.println(s);
dos.close();
dis.close();
//dos.flush();
s.close();
}
catch (UnknownHostException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
public class TcpServer
{
public static void main(String[] args)
{
try {
ServerSocket ss = new ServerSocket(6970);
while(true)
{
Socket socket = ss.accept();
InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
DataInputStream dis = new DataInputStream(is);
String s = null;
if((s=dis.readUTF())!=null)
{
System.out.println(s);
}
dos.writeUTF("恩,你也好");
// dos.flush();
dis.close();
dos.close();
socket.close();
//ss.close();
}
}
catch(IOException e)
{e.printStackTrace();}
}
} 展开
public class TcpClient
{
public static void main(String[] args)
{
try {
Socket s = new Socket("localhost",6970);
InputStream is = s.getInputStream();
OutputStream os = s.getOutputStream();
DataInputStream dis = new DataInputStream(is);
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF("你好");
String ss = null;
if((ss=dis.readUTF())!=null);
System.out.println(s);
dos.close();
dis.close();
//dos.flush();
s.close();
}
catch (UnknownHostException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
public class TcpServer
{
public static void main(String[] args)
{
try {
ServerSocket ss = new ServerSocket(6970);
while(true)
{
Socket socket = ss.accept();
InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
DataInputStream dis = new DataInputStream(is);
String s = null;
if((s=dis.readUTF())!=null)
{
System.out.println(s);
}
dos.writeUTF("恩,你也好");
// dos.flush();
dis.close();
dos.close();
socket.close();
//ss.close();
}
}
catch(IOException e)
{e.printStackTrace();}
}
} 展开
2个回答
展开全部
两个main method。第二个跑起来的时候,第一个socket 已经timeout了
追问
不是,我这两个是两个类,不是写在一起的。
追答
System.out.println(s);
这里 再打一行:
System.out.println(ss);你最后放的值 在ss里面。
CLIENT CLASS 里面:
String ss = null;
if((ss=dis.readUTF())!=null);
System.out.println(s);
这里 SS都做了判断,为何却打印了S?打印SS才对。 楼主给分
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询