Java—实现区域网内聊天
1、获取本机IP
package com.hwadee;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Test1 {
public static void main(String[] args) throws UnknownHostException {
InetAddress inetAddress=InetAddress.getByName("这儿填写自己电脑的名称");
String hostAddress = inetAddress.getHostAddress();
String hostName = inetAddress.getHostName();
System.out.println(inetAddress);
System.out.println(hostAddress);
System.out.println(hostName);
}
}
2、实现本机内互聊:必须先把接收端打开,再运行发送端(只要在同区域网下,修改计算机名和端口号,即可实现互相发送和接收)(UDP)
package com.cn;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class Receive {
public static void main(String[] args) throws IOException {
DatagramSocket ds=new DatagramSocket(10088);
byte[] buf=new byte[1024*1];
int length=buf.length;
DatagramPacket dp=new DatagramPacket(buf, length);
ds.receive(dp);
byte[] data = dp.getData();
int len = dp.getLength();
String message=new String(data, 0, len);
InetAddress address = dp.getAddress();
String hostAddress = address.getHostAddress();
System.out.println("我收到了客户端发来的消息:"+message+",客户端的ip:"+hostAddress);
//ds.close(); //服务器一般而言,是不能关闭的
}
}
package com.cn;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class Send {
public static void main(String[] args) throws IOException {
DatagramSocket ds=new DatagramSocket();
String message="今天晚上有月全食,150年一遇";
byte[] bs = message.getBytes();
int length=bs.length;
InetAddress address=InetAddress.getByName("PC-C-2");
DatagramPacket p=new DatagramPacket(bs, length, address, 10088);
ds.send(p);
ds.close();
}
}
3、实现不间断地发送和接收(不需要每次发送和接收都重启接收端)(UDP)
package com.liaotianshi1;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class TestReceived {
public static void main(String[] args) throws IOException {
DatagramSocket ds=new DatagramSocket(10088);
while(true) {
byte[] buf=new byte[1024*1];
int length=buf.length;
DatagramPacket dp=new DatagramPacket(buf, length);
ds.receive(dp);
byte[] data = dp.getData();
int len = dp.getLength();
String message=new String(data, 0, len);
InetAddress address = dp.getAddress();
String hostAddress = address.getHostAddress();
System.out.println("我收到了客户端发来的消息:"+message+",客户端的ip:"+hostAddress);
//ds.close(); //服务器一般而言,是不能关闭的
}
}
}
package com.liaotianshi1;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;
public class TestSend {
public static void main(String[] args) throws IOException {
DatagramSocket ds=new DatagramSocket();
Scanner scanner=new Scanner(System.in);
String line=null;
while((line=scanner.nextLine())!=null) {
byte[] bs = line.getBytes();
int length=bs.length;
InetAddress address=InetAddress.getByName("PC-C-2");
DatagramPacket p=new DatagramPacket(bs, length, address, 10088);
ds.send(p);
}
ds.close();
scanner.close();
}
}
4、实现本机内互聊(TCP)
package com.liaotianshi2;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws IOException {
InetAddress address=InetAddress.getByName("PC-C-2");
int port=10087;
Socket socket=new Socket(address,port);
OutputStream outputStream=socket.getOutputStream();
outputStream.write("月全食在成都可能看不到".getBytes());
InputStream inputStream=socket.getInputStream();
byte[] b=new byte[1024*1];
int len = inputStream.read(b);
String message=new String(b, 0, len);
System.out.println("客户端收到服务端的反馈--->"+message);
inputStream.close();
socket.close();
}
}
package com.liaotianshi2;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket ss=new ServerSocket(10087);
Socket socket = ss.accept();
InputStream inputStream=socket.getInputStream();
byte [] bs=new byte[1024*1];
int len=inputStream.read(bs);
String message=new String(bs, 0, len);
String hostAddress = socket.getInetAddress().getHostAddress();
System.out.println("这是客户端的消息--->"+message+",来自"+hostAddress);
OutputStream outputStream=socket.getOutputStream();
outputStream.write("已收到客户端的消息".getBytes());
outputStream.close();
//ss.close();
}
}
5、实现不间断地本机的发送与接收(TCP)
package com.liaotianshi3;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;
public class TestClient {
public static void main(String[] args) throws IOException {
InetAddress address=InetAddress.getByName("PC-C-2");
int port=10082;
Scanner scanner=new Scanner(System.in);
String line=null;
while((line=scanner.nextLine())!=null) {
Socket socket=new Socket(address,port);
OutputStream outputStream=socket.getOutputStream();
outputStream.write(line.getBytes());
InputStream inputStream=socket.getInputStream();
byte[] b=new byte[1024*1];
int len = inputStream.read(b);
String message=new String(b, 0, len);
System.out.println("客户端收到服务端的反馈--->"+message);
inputStream.close();
}
//socket.close();
}
}
package com.liaotianshi3;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* Connection refused: connect
* 在使用TCP的时候,一定要确保服务端先运行起来
* Address already in use: JVM_Bind
* 这个错误是端口号已经被使用,可以重启ecplise
*/
public class TestServer {
public static void main(String[] args) throws IOException {
ServerSocket ss=new ServerSocket(10082);
while(true) {
Socket socket = ss.accept();
InputStream inputStream=socket.getInputStream();
byte [] bs=new byte[1024*1];
int len=inputStream.read(bs);
String message=new String(bs, 0, len);
String hostAddress = socket.getInetAddress().getHostAddress();
System.out.println("这是客户端的消息--->"+message+",来自"+hostAddress);
OutputStream outputStream=socket.getOutputStream();
outputStream.write("已收到客户端的消息".getBytes());
outputStream.close();
inputStream.close();
socket.close();
}
//ss.close();
}
}