--------------------------简单的交互----------------------------------
InetAddress
public class InetAddress {
public static void main(String[] args) {
try {
java.net.InetAddress id = java.net.InetAddress.getByName("WWW.baidu.com");
System.out.println(id.getHostAddress());
System.out.println(id.getLocalHost().getHostAddress());
System.out.println(id.getLocalHost().getHostName());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
socket
public class Server {
static int c = 0;
public static void main(String[] args) {
try {
if(c==0){
System.out.println(c+"if");
c++;
}else {
System.out.println(c+"else");
}
ServerSocket server = new ServerSocket(10010);
System.out.println("服务器已经启动");
Socket socket = server.accept();
InputStream in = socket.getInputStream();
byte[] b = new byte[1024];
int len = in.read(b);
String s = new String(b,0,len);
System.out.println("客户端发送的信息:"+ s);
OutputStream out = socket.getOutputStream();
out.write("hello,我是服务器".getBytes());
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class Client {
public static void main(String[] args) {
try {
InetAddress iad = InetAddress.getLocalHost();
Socket socket = new Socket(iad.getHostAddress(), 10010);
OutputStream out = socket.getOutputStream();
String message = "Hello,I am ls";
out.write(message.getBytes());
InputStream in = socket.getInputStream();
byte[] b = new byte[1024];
int len = in.read(b);
String a = new String (b,0,len);
System.out.println("接收服务端数据未:" + a);
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
datagram
public class client {
public static void main(String[] args) {
String romoteHost = "LAPTOP-5LRJIHE3";
try {
InetAddress address = InetAddress.getByName(romoteHost);
int port = 10011;
byte[] data = "账号:admin,密码:123".getBytes();
DatagramPacket datagramPacket = new DatagramPacket(data, data.length,address,port);
DatagramSocket socket = new DatagramSocket();
socket.send(datagramPacket);
byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
String content = new String(packet.getData(),0,packet.getLength());
System.out.println("接收到服务器端的数据:"+content);
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class Server {
public static void main(String[] args) {
try {
DatagramSocket socket = new DatagramSocket(10011);
System.out.println("服务器已经启动!");
byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
String content = new String(packet.getData(),0,packet.getLength());
System.out.println("获取客户端的数据未:"+content);
System.out.println(packet.getAddress());
System.out.println(packet.getPort());
InetAddress address = packet.getAddress();
int port = packet.getPort();
byte[] data = "欢迎你".getBytes();
DatagramPacket datagramPacket = new DatagramPacket(data, data.length, address, port);
socket.send(datagramPacket);
socket.close();
} catch (SocketException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
}
------------多次交互---------------------------------------------
package demo3;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Client {
public static void main(String[] args) {
OutputStream os = null;
InputStream is = null;
Socket s = null;
Scanner input = new Scanner(System.in);
String str = null;
int count = 0;
try {
System.out.println("客户端");
s = new Socket("127.0.0.1", 1766);
while(!"q".equals(str)){
System.out.println("一个客户端和一个服务器的第"+(++count)+"次交互");
System.out.println("客户端发送:(q结束交互)");
str = input.next();
os = s.getOutputStream();
byte[] osBuf = str.getBytes();
os.write(osBuf);
is = s.getInputStream();
byte[] isBuf = new byte[512];
int index = is.read(isBuf);
System.out.println("客户端接收:" + new String(isBuf, 0, index));
}
System.out.println("客户端断开连接!");
os.close();
is.close();
s.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package demo3;
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) {
ServerSocket listener = null;
Socket s = null;
InputStream is = null;
OutputStream os = null;
String str = null;
int count = 0;
try {
listener = new ServerSocket(1766);
System.out.println("服务端等待连接中。。。");
s = listener.accept();
while(!"q".equals(str)){
System.out.println("一个客户端和一个服务器的第"+(++count)+"次交互");
is = s.getInputStream();
byte[] isBuf = new byte[512];
int index = is.read(isBuf);
str = new String(isBuf, 0, index);
System.out.println("服务器接收:" +str);
os = s.getOutputStream();
byte[] osBuf = "我已收到".getBytes();
System.out.println("服务器发送:"+new String(osBuf,0,osBuf.length));
os.write(osBuf);
}
System.out.println("服务器端断开连接!");
is.close();
os.close();
s.close();
listener.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}