基于tcp/ip协议_BIO:
socket客户端:
public class SocketClient {
public static void main(String[] args) {
try {
Socket socket=new Socket("localhost",8888);
BufferedReader reader=new BufferedReader(new InputStreamReader(socket.getInputStream())); //读取服务端信息
PrintWriter writer=new PrintWriter(socket.getOutputStream(),true); //往服务端写数据
writer.println("Hello word");
writer.flush();
while(true){
String serverData=reader.readLine();
if(serverData==null){
break;
}
System.out.println("客户端收到数据:"+serverData);
}
writer.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
服务端:
public class SocketServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket=null;
try{
//启动服务,端口为8888
serverSocket=new ServerSocket(8888);
while(true){
Socket socket=serverSocket.accept(); //等待一个接收请求
new Thread(()->{
try {
BufferedReader reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter writer =new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
while(true){
String clientData=reader.readLine(); //读取客户端发送过来的消息
if(clientData==null){
break;
}
System.out.println("服务端接收到的数据:"+clientData);
writer.println("Hello 001");
writer.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}).start();
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(serverSocket!=null){
serverSocket.close();
}
}
}
}
基于udp/ip协议_BIO:
DatagramSocket客户端:
public static void main(String[] args) {
DatagramSocket socket = null;
try {
socket = new DatagramSocket();
//发送消息,需要设置对方ip及端口
String msg = "hello 001";
InetAddress inet = InetAddress.getByName("localhost");
DatagramPacket sendpacket = new DatagramPacket(msg.getBytes(), msg.length(),inet,8989);
socket.send(sendpacket);
byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf, buf.length, inet, 8989);
socket.receive(packet);//进入监听,等待接收消息
System.out.println("接收到消息:"+new String(buf,0,packet.getLength()));
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (socket != null) {
socket.close();
}
}
}
服务端:
public static void main(String[] args) {
DatagramSocket socket = null;
try {
socket = new DatagramSocket(8989);
byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);//进入监听,等待接收消息
System.out.println("接收到消息:"+new String(buf,0,packet.getLength()));
//发送消息,需要设置对方ip及端口
String msg = "hello 002";
DatagramPacket sendpacket = new DatagramPacket(msg.getBytes(), msg.length(), packet.getAddress(), packet.getPort());
socket.send(sendpacket);
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if (socket != null) {
socket.close();
}
}
}
组播
客户端:
InetAddress group=InetAddress.getByName("224.5.6.7");
MulticastSocket socket = null;
try {
socket=new MulticastSocket(8888);
socket.joinGroup(group); //加到指定的组里面
byte[] buf=new byte[1024];
while(true){
DatagramPacket packet=new DatagramPacket(buf,buf.length);
socket.receive(packet);
System.out.println("接收到的数据:"+new String(packet.getData()));
System.out.println("接收到的数据:"+new String(buf));
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if (socket !=null) {
socket.close();
}
}
}
服务端:
public static void main(String[] args) {
try {
//地址段:224.0.0.0 - 239.255.255.255
InetAddress group=InetAddress.getByName("224.5.6.7");
MulticastSocket socket=new MulticastSocket();
for(int i=0;i<10;i++){
String data="Hello all";
byte[] bytes=data.getBytes();
socket.send(new DatagramPacket(bytes,bytes.length,group,8888));
TimeUnit.SECONDS.sleep(2);
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
网友评论