public class Client {
public static void main(String[] args) throws Exception {
//创建客户端对象(发韭菜出去的人)
DatagramSocket socket = new DatagramSocket();
/*DatagramPacket(byte buf[], int offset, int length,
InetAddress address, int port)
参数一:封装要发出去的数据
参数二:发出去的数据大小(字节个数)
参数三:服务端的IP地址
参数四:服务端程序的接口
*/
Scanner sc =new Scanner(System.in);
while (true) {
String msg = sc.nextLine();
if("exit".equals(msg)){
System.out.println("关闭客户端");
socket.close();
break;
}
byte[] bytes = msg.getBytes();
//创建数据包对象封装要发出的数据
DatagramPacket packet = new DatagramPacket(bytes,bytes.length,InetAddress.getLocalHost(),6666);
//发送数据
socket.send(packet);
System.out.println("数据发送完毕");
}
}
}
public class Server {
public static void main(String[] args) throws Exception {
System.out.println("--服务端--");
//创建一个服务端(接韭菜的人) 注册端口
DatagramSocket socket = new DatagramSocket(6666);
//创建一个数据包,用于接收对象
byte[] buffer = new byte[1024 * 64];
DatagramPacket packet = new DatagramPacket(buffer,buffer.length);
while (true) {
//接收数据
socket.receive(packet);
//获取接收了多少数据
int length = packet.getLength();
String s = new String(buffer, 0, length);
System.out.println(s);
//获取客户端端口和地址
System.out.println(packet.getAddress().getHostAddress());
System.out.println(packet.getPort());
}
}
}
TCP通信
public class Client {
public static void main(String[] args) throws Exception {
//创建Socket对象,并同时请求与服务端连接
Socket socket = new Socket(InetAddress.getLocalHost(),8888);
//从Socket管道中得到输出流,用来发送数据
OutputStream out = socket.getOutputStream();
//创建数据输出流对象
DataOutputStream d = new DataOutputStream(out);
Scanner sc = new Scanner(System.in);
while (true) {
//传输数据
System.out.println("请输入");
String s = sc.nextLine();
if("exit".equals(s)){
System.out.println("退出系统!");
d.close();
socket.close();
break;
}
d.writeUTF(s);
d.flush();
}
}
}
public class Server {
public static void main(String[] args) throws Exception {
//创建ServerSocket对象,并注册端口
ServerSocket serverSocket = new ServerSocket(8888);
//等待客户端连接
Socket socket = serverSocket.accept();
//从Socket中得到一个字节输入流,用来接收数据
InputStream in = socket.getInputStream();
//创建数据输入流,和前面保持一致
DataInputStream d = new DataInputStream(in);
//读入数据
while (true) {
try {
String s = d.readUTF();
System.out.println(s);
} catch (IOException e) {
System.out.println(socket.getRemoteSocketAddress()+"离线了");
d.close();
socket.close();
break;
}
}
}
}
多个客户端
public class Server {
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(8888);
//多线程接收多个客户端的数据
while(true){
Socket socket = serverSocket.accept();
System.out.println(socket.getRemoteSocketAddress()+"上线了");
new ServerReadSocket(socket).start();
}
}
}
public class ServerReadSocket extends Thread{
private Socket socket;
public ServerReadSocket(Socket socket){
this.socket=socket;
}
@Override
public void run() {
try {
InputStream in = socket.getInputStream();
DataInputStream d = new DataInputStream(in);
while(true){
try {
String s = d.readUTF();
System.out.println(s);
} catch (Exception e) {
System.out.println(socket.getRemoteSocketAddress()+"下线了");
socket.close();
d.close();
break;
}
}
} catch (IOException e) {
System.out.println(e.getLocalizedMessage());
}
}
}