网络基础

mch20093493 2012-04-23 02:44:24
在java中,客户机和服务器实现的具体方法是什么?它们是怎么运行的?以下面的代码来解释一下……
package test4;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerDemo {
public static void main(String[] args) {
ServerSocket server = null;
Socket clientSocket = null;
String str = null;
DataOutputStream out = null;
DataInputStream in = null;
try {
server = new ServerSocket(4331);
clientSocket = server.accept();
in = new DataInputStream(clientSocket.getInputStream());
out = new DataOutputStream(clientSocket.getOutputStream());
while(true){
str = in.readUTF();//从内存读取
out.writeUTF("hello,我是服务器");//写入内存
out.writeUTF(str);//在将读到的值写入内存
System.out.println("服务器收到:"+str);
Thread.sleep(1000);
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}

}
}
package test4;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

public class SocketUtil {
public static void main(String[] args) {
String str =null;
Socket clientSocket;
DataInputStream in ;
DataOutputStream out;
try {
clientSocket = new Socket("localhost",4331);
in = new DataInputStream(clientSocket.getInputStream());
out = new DataOutputStream(clientSocket.getOutputStream());
out.writeUTF("你好");//写入内存
while(true){
str = in.readUTF();//从内存读取数据
out.writeUTF(((int)(Math.random()*10)+1)+"");//写入内存
System.out.println("客户端收到:"+str);
Thread.sleep(1000);
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}
谢谢高手赐教……
...全文
56 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
Aria_zuo 2012-04-24
  • 打赏
  • 举报
回复

public class ServerDemo {
public static void main(String[] args) {
ServerSocket server = null; //声明ServerSocket对象
Socket clientSocket = null;//声明一个Socket对象表示客户端
String str = null;
DataOutputStream out = null; //输出流
DataInputStream in = null;//输入流
try {
server = new ServerSocket(4331);//此服务器在4331的端口上等待客户端的访问
clientSocket = server.accept(); //等待客户端的链接
in = new DataInputStream(clientSocket.getInputStream()); //获取客户端的输入流
out = new DataOutputStream(clientSocket.getOutputStream());//客户端的输入流
while(true){
str = in.readUTF();//从内存读取
out.writeUTF("hello,我是服务器");//写入内存
out.writeUTF(str);//在将读到的值写入内存
System.out.println("服务器收到:"+str);
Thread.sleep(1000);
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}

}
}

因为程序执行到 accept()方法后就进入了阻塞状态,此阻塞状态会在客户端连接之后改变。

public class SocketUtil {
public static void main(String[] args) {
String str =null;
Socket clientSocket;
DataInputStream in ;
DataOutputStream out;
try {
clientSocket = new Socket("localhost",4331);//指定连接的主机和端口
in = new DataInputStream(clientSocket.getInputStream());//获取客户端的输入信息
out = new DataOutputStream(clientSocket.getOutputStream());//客户端的输出信息
out.writeUTF("你好");//写入内存
while(true){
str = in.readUTF();//从内存读取数据
out.writeUTF(((int)(Math.random()*10)+1)+"");//写入内存
System.out.println("客户端收到:"+str);
Thread.sleep(1000);
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}


根据你的代码执行程序应该是:
服务器端:
服务器收到:你好
服务器收到:5
服务器收到:2
服务器收到:6
服务器收到:9
服务器收到:5
服务器收到:9
服务器收到:8...
客户端:
客户端收到:hello,我是服务器
客户端收到:你好
客户端收到:hello,我是服务器
客户端收到:5
客户端收到:hello,我是服务器
客户端收到:2
客户端收到:hello,我是服务器
客户端收到:6
总的流程应该就是:
首先服务器端,等待客户端的链接,链接后,获得客户端的输入,(察看客户端发现,客户端输入信息为“你好”和10以内的随机数)。
客户端而言,当链接服务器后,首先读取到服务器输入的数据,然后是自己输入的信息。

50,545

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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