服务器端Socket程序
package com.aaa.servet;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;//导入java.net包,用于网络通讯
import java.net.Socket;
public class ServetSocketTest {
public static void main(String[] args) {
System.out.println("...启动服务器...");
//1.初始化服务端Socket并绑定端口为8888
try {
ServerSocket serverSocket=new ServerSocket(8888);//需要添加try/catch块
//2.等待客户端的连接
Socket socket=serverSocket.accept();
//3.获取输入流,通过socket对象流,传递给缓冲字符流进行读取;
BufferedReader bufferReader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//4.读取一行数据;
String str=bufferReader.readLine();
//5.输出打印
System.out.println(str);
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.aaa.clicent;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class ClientSocket {
public static void main(String[] args) {
//1.创建socket对象,建立到服务i端的连接;
try {
Socket socket=new Socket("127.0.0.1",8888);
//2.通过缓冲输出流,包装下socket对象的输出字节流,进行写操作;
BufferedWriter bufferWriter=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
//3.构造字符串,写进去
String str="你是我的小心肝";
bufferWriter.write(str);
//3.2还需要将缓冲中的内容放到socket
bufferWriter.flush();
//4.注意一定要有关闭连接;
socket.shutdownOutput();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
5.现在需要继续升级,如何循环!!!可以多个循环;
服务器端修改为:
客户端修改为:
--------UDP Socket连接---
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class UDPServer {
public static void main(String[] args) {
//1.创建服务器端的DatagramSocket数据包,
try {
DatagramSocket datagramSocket=new DatagramSocket(8800);
//2.创建数据报,用于接受用户传送过来的数据;存放于字节数组;
byte[]bytes=new byte[1024];
DatagramPacket datagramPacket=new DatagramPacket(bytes,bytes.length);
System.out.println("*****服务器端已经启动****");
//使用receive方法接受;
datagramSocket.receive(datagramPacket);
//4.读取数据;
String info=new String(bytes,0,datagramPacket.getLength());
//5.输出信息;
System.out.println("服务器,客户端信息为:"+info);
//升级,服务器端响应客户端;
InetAddress address=datagramPacket.getAddress();
int port=datagramPacket.getPort();
byte[]sinfo="欢迎您".getBytes();
//升级2.创建数据报,包含socket信息;
DatagramPacket packet=new DatagramPacket(sinfo,sinfo.length,address,port);
// 升级3;
datagramSocket.send(packet);
//关闭资源;
datagramSocket.close();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
==客户端
import java.io.IOException;
import java.net.*;
public class UDPClient {
public static void main(String[] args) {
//1.定义服务器的地址、端口号、数据;
try {
InetAddress address=InetAddress.getByName("localhost");
int port=8800;
byte[]data="用户名:admin,密码:123".getBytes();
//2.创建数据报,包含发送的数据信息,可以使用带有4个参数的方法;
DatagramPacket datagramPacket=new DatagramPacket(data,data.length,address,port);
//3.创建DatagramSocket对象;
DatagramSocket datagramSocket=new DatagramSocket();
//4.向服务器发送数据;
datagramSocket.send(datagramPacket);
////升级
byte[]data2=new byte[1024];
DatagramPacket packet2=new DatagramPacket(data2,data2.length);
//2.接受服务器响应的数据 ;
datagramSocket.receive(packet2);
//3.读取数据;
String reply=new String(data2,0,packet2.getLength());
System.out.println("我是客户端,服务器端说:"+reply);
//4.关闭资源;
datagramSocket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}