局域网:一般IP地址范围192.168.0.0 - 192.168.255.255
本机:127.0.0.1,域名localhost
java内使用InetAddress类代表IP地址
该类将构造方法私有化了,所以说不能直接new对象
创建对象:InetAddress p = InetAddress.getByName("域名或者IP")
p.getHostName() 返回p的域名
p.getHostAddress() 返回p的IP地址
获取本机IP地址:InetAddress.getLocalHost()
将socket对象转换为InetAddress对象:socket.getInetAddress()
端口号:0~65535。前1024个被预先占用了
TCP:三次握手连接,四次挥手断开
客户端,服务器
TCP实现一个最简单的通信,先运行服务器代码,在运行客户端代码
客户端
public class test {
public static void main(String[]args) throws IOException{
InetAddress ia = InetAddress.getByName("127.0.0.1");
try(Socket so = new Socket(ia , 12000); 向服务器的端口号发起连接
OutputStream ou = so.getOutputStream();){
ou.write("你好,我是客户端".getBytes());
}
catch(IOException e) {
System.out.println("出错了");
}
}
}
服务器
public class sermain {
public static void main(String[]args) throws IOException {
ServerSocket sr = new ServerSocket(12000); 服务器一直等待客户端的连接
try(Socket so = sr.accept(); 获取连接的socket对象
InputStream is = so.getInputStream();
InputStreamReader ir = new InputStreamReader(is , "utf-8");){ 使用转化流,避免出现乱码
char[] by = new char[2];
int in = ir.read(by);
while(in != -1) {
String s = new String(by , 0 , in);
System.out.print(s);
in = ir.read(by);
}
}
catch(Exception e) {
System.out.println("结束1");
}
}
}
代码2.0
socket的输入或者输出不会自己中断,需要手动中断
shutdownOutput,shutdownInput
客户端
public class test {
public static void main(String[]args) throws IOException{
InetAddress ia = InetAddress.getByName("127.0.0.1");
try(Socket so = new Socket(ia , 12000);
OutputStream ou = so.getOutputStream();
BufferedInputStream bi = new BufferedInputStream(new FileInputStream("./hhh.txt"));
InputStream it = so.getInputStream();
BufferedInputStream bit = new BufferedInputStream(it);){
byte[] by = new byte[10];
int len = bi.read(by);
while(len != -1) {
ou.write(by , 0 , len);
len = bi.read(by);
}
so.shutdownOutput(); 就这里,需要中断写之后才可以读
byte[] lcy = new byte[100];
int qwert = bit.read(lcy);
String sts = new String(lcy , 0 , qwert);
System.out.println("结束啦啦啦啦啦啦啦啦啦啦啦啦啦啦");
System.out.println(sts);
}
catch(IOException e) {
System.out.println(e.getMessage());
}
}
}
public class sermain {
public static void main(String[]args) throws IOException {
ServerSocket sr = new ServerSocket(12000);
try(Socket so = sr.accept();
InputStream is = so.getInputStream();
InputStreamReader ir = new InputStreamReader(is , "utf-8");
BufferedWriter buf = new BufferedWriter(new FileWriter("bea.txt"));
OutputStream out = so.getOutputStream();
BufferedOutputStream bout = new BufferedOutputStream(out)){
char[] by = new char[5];
int in = ir.read(by);
while(in != -1) {
buf.write(by , 0 , in);
in = ir.read(by);
}
so.shutdownInput(); 不写也可以,只客户端写就行了
bout.write("服务器收到".getBytes());
}
catch(Exception e) {
System.out.println("结束1");
}
}
}
UDP实现简单的通信:
客户端
package book;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
//客户端
public class UDPclient {
public static void main(String[]args) throws Exception{
DatagramSocket socket = new DatagramSocket();
try(InputStreamReader input = new InputStreamReader(System.in);
BufferedReader bfa = new BufferedReader(input);){
String str = bfa.readLine();
byte[] by = str.getBytes();
InetAddress inet = InetAddress.getByName("localhost");
DatagramPacket pack = new DatagramPacket(by , 0 , by.length , inet , 12000);
socket.send(pack);
}
socket.close();
}
}
服务器
package book;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
//服务器端
public class UDPservice {
public static void main(String[]args) throws Exception{
DatagramSocket socket = new DatagramSocket(12000);
byte[] buffer = new byte[100];
DatagramPacket packet = new DatagramPacket(buffer , 0 , buffer.length);
socket.receive(packet);
System.out.println(new String(packet.getData() , 0 , packet.getLength()));
socket.close();
}
}