- tcp
public class Client {
public static void main(String[] args) throws Exception{
while (true){
System.out.println("输入1查看新消息\n输入2发送消息");
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
if(i==1){
Socket socket = new Socket(InetAddress.getByName("10.36.140.123"),7777);
wirteMessage(socket,null,null);
getMessage(socket);
socket.close();
}
else if(i==2){
scanner = new Scanner(System.in);
System.out.println("请输入要发送消息的用户ip");
String targetIp = scanner.next();
System.out.println("请输入发送的消息");
String body = scanner.next();
Socket socket2 = new Socket(InetAddress.getByName("10.36.140.123"),7777);
wirteMessage(socket2, targetIp, body);
}else System.out.println("请重新输入");
}
}
private static void wirteMessage(Socket socket, String targetIp, String body) throws IOException {
OutputStream outputStream = socket.getOutputStream();
if(targetIp!=null&&body!=null)
outputStream.write((targetIp+":"+body).getBytes());
else outputStream.write(("null").getBytes());
outputStream.flush();
}
private static void getMessage(Socket socket) throws IOException {
InputStream inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
String s = new String(Arrays.copyOf(bytes,inputStream.read(bytes)));
System.out.println(s+"end");
}
}
public class Service{
public static void main(String[] args) throws Exception{
ServerSocket serverSocket = new ServerSocket(7777);
ArrayList<Message> messages = new ArrayList<>();
while (true){
System.out.println("等待客户端连接");
Socket socket = serverSocket.accept();
String ip = socket.getInetAddress().getHostAddress();
System.out.println("访问的ip为"+ip);
InputStream inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
int len = inputStream.read(bytes);
socket.shutdownOutput();
String s = new String(Arrays.copyOf(bytes,len));
System.out.println(s);
String[] split = s.split(":");
if(split.length!=2){
OutputStream outputStream = socket.getOutputStream();
boolean flag = true;
for (int i=0;i<messages.size();i++) {
if(messages.get(i).targetIp.equals(ip)){
outputStream.write((messages.get(i).ip+"新消息\n"+messages.get(i).body).getBytes());
messages.remove(i);
flag = false;
break;
}
}
if(flag){
outputStream.write("没有新消息".getBytes());
}
outputStream.flush();
}
else messages.add(new Message(socket.getInetAddress().getHostAddress(),split[0],split[1]));
System.out.println(messages);
}
}
static class Message{
String ip;
String targetIp;
String body;
public Message(String ip, String targetIp, String body) {
this.ip = ip;
this.targetIp = targetIp;
this.body = body;
}
public Message() {
}
@Override
public String toString() {
return "Message{" +
"ip='" + ip + '\'' +
", targetIp='" + targetIp + '\'' +
", body='" + body + '\'' +
'}';
}
}
}
- udp
public class Client {
public static void main(String[] args) throws Exception{
Scanner scanner = new Scanner(System.in);
System.out.println("请输入接受方的Ip");
String next = scanner.next();
System.out.println("请输入发送的内容");
String body = scanner.next();
sendMessage(next,6666,body);
}
private static void sendMessage(String next, int port, String body) throws Exception{
DatagramSocket socket = new DatagramSocket();
DatagramPacket packet = new DatagramPacket(body.getBytes(), body.length(), InetAddress.getByName(next), port);
socket.send(packet);
}
}
public class Service {
public static void main(String[] args) throws Exception{
Message message = readPacket();
System.out.println(message);
}
private static Message readPacket() throws IOException {
DatagramSocket socket = new DatagramSocket(6666);
byte[] bytes = new byte[1024];
DatagramPacket packet = new DatagramPacket(bytes, bytes.length);
socket.receive(packet);
String s = new String(packet.getData());
System.out.println(s);
Message message = new Message();
message.ip = packet.getAddress().getHostAddress();
System.out.println(message);
String[] split = s.split("=|&");
for (int i = 0; i < split.length; i++) {
if("ip".equals(split[i])){
message.targetIp = split[i+++1];
}else if("message".equals(split[i])){
message.body = split[i++ +1];
}
}
return message;
}
static class Message{
String ip;
String targetIp;
String body;
public Message(String ip, String targetIp, String body) {
this.ip = ip;
this.targetIp = targetIp;
this.body = body;
}
public Message() {
}
@Override
public String toString() {
return "Message{" +
"ip='" + ip + '\'' +
", targetIp='" + targetIp + '\'' +
", body='" + body + '\'' +
'}';
}
}
}
|