在java网络编程中包含两种方式,Socket(TCP)和DatagramSocket(UDP)。
在网络编程中常用的三个类
InetAddress:用于标识网络上的硬件资源;
Socket:使用TCP协议实现网络通信的Socket相关的类;
DatagramSocket:使用UDP协议将数据存在数据报中通过网络进行通信;
Socket和DatagramSocket的区别
Socket使用的tcp连接,需要先连接之后才能发送数据;
DatagramSocket使用的UDP连接,客户端不需要先连接数据,可以直接发送给指定服务端。
实例:
public class TcpServer {
private static ServerSocket serverSocket;
private static Socket socket;
public static void startServer(){
if (serverSocket == null){
new Thread(new Runnable() {
@Override
public void run() {
try {
serverSocket = new ServerSocket(8888);
System.out.println("启动服务端,等待客户端连接中");
socket = serverSocket.accept();
System.out.println("客户端连接上来了");
sendTcpMessage("你好,我是服务端");
InputStream inputStream = socket.getInputStream();
byte[] buffer = new byte[1024];
int len = -1;
while ((len = inputStream.read(buffer)) != -1) {
String data = new String(buffer, 0, len);
System.out.println("收到客户端的数据:" + data);
}
// socket.getOutputStream().write("你好,服务端已接收到您的信息".getBytes());
// socket.getOutputStream().flush();
// socket.shutdownOutput();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
socket = null;
serverSocket = null;
}
}
}).start();
}
}
public static void sendTcpMessage(final String msg){
if (socket != null && socket.isConnected()) {
new Thread(new Runnable() {
@Override
public void run() {
try {
socket.getOutputStream().write(msg.getBytes());
socket.getOutputStream().flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
socket.shutdownOutput();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
}
public class TcpClient {
public static Socket socket;
public static void startClient(final String address, final int port) {
if (socket == null) {
new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println("启动客户端");
socket = new Socket(address, port);
System.out.println("客户端连接服务器成功");
sendTcpMessage("你好,我是客户端");
PrintWriter pw = new PrintWriter(socket.getOutputStream());
InputStream inputStream = socket.getInputStream();
byte[] buffer = new byte[1024];
int len = -1;
while ((len = inputStream.read(buffer)) != -1) {
String data = new String(buffer, 0, len);
System.out.println("收到服务器的数据:" + data);
}
System.out.println("客户端连接服务器断开");
pw.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("客户端连接服务器失败");
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
socket = null;
}
}
}).start();
}
}
public static void sendTcpMessage(final String msg) {
if (socket != null && socket.isConnected()) {
new Thread(new Runnable() {
@Override
public void run() {
try {
socket.getOutputStream().write(msg.getBytes());
socket.getOutputStream().flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
socket.shutdownOutput();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
public static void main(String[] args) {
startClient("127.0.0.1",8888);
}
}
测试