UDP server
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException; /**
* udpserver
* 监听端口 10001
* @author GXF
*
*/
public class UdpSocketServer {
private DatagramSocket serverSocket; public UdpSocketServer(int port){
init(port);
} public void init(int port){ InetSocketAddress serverSocketAddress = new InetSocketAddress( port);
try {
serverSocket = new DatagramSocket(serverSocketAddress);
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} //监听端口
public void startListen(){
byte buff[] = new byte[1024];
DatagramPacket receivePacket = new DatagramPacket(buff, buff.length);
while(true){
try {
serverSocket.receive(receivePacket);
String message = new String(buff, 0, receivePacket.getLength());
System.out.println("message from client = " + message);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} public static void main(String args[]){
UdpSocketServer udpSocketServer = new UdpSocketServer(10001);
udpSocketServer.startListen();
}
}
UDP client
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.net.UnknownHostException; /**
* Udp客户端 DatagramSocket
* @author GXF
*
*/
public class UdpSocketClient {
//客户端套接字
private DatagramSocket clientSocket; //构造方法
public UdpSocketClient(String ip, int port){
init(ip, port);
} /**
* 初始化套接字
* @param ip
* @param port
*/
public void init(String ip, int port){
// try {
// InetAddress serverAddress = InetAddress.getByName(ip);
// InetSocketAddress serverSocketAddress = new InetSocketAddress(serverAddress, port);
try {
clientSocket = new DatagramSocket();
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// } catch (UnknownHostException | SocketException e) {
// e.printStackTrace();
// }
} /**
* 使用UDP向服务器发送消息
* @param ip
* @param message
*/
public void sendMessage(String message){
byte buff[] = message.getBytes();
DatagramPacket packetToSend;
try {
packetToSend = new DatagramPacket(buff, buff.length, InetAddress.getByName("127.0.0.1"), 10002);
clientSocket.send(packetToSend); //发送数据包
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public static void main(String[] args) {
String serverIp = "127.0.0.1";
int port = 10001;
UdpSocketClient clientSocket = new UdpSocketClient(serverIp, port);
String message = "hahahahah";
clientSocket.sendMessage(message); } }
TCP server
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket; public class TcpServer {
private ServerSocket serverSocket; TcpServer(int port){
try {
serverSocket = new ServerSocket(port);
} catch (IOException e) {
e.printStackTrace();
}
} //开始监听端口
public void start(){
while(true){
try {
Socket clientSocket = serverSocket.accept();
InputStream is = clientSocket.getInputStream();
byte buff[] = new byte[1024];
int length = -1;
StringBuilder sb = new StringBuilder();
// while((length = is.read(buff)) > 0){
length = is.read(buff);
sb.append(new String(buff, 0, length));
// }//while
System.out.println("message received: " + sb.toString());
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
} }
} public static void main(String args[]){
int port = 10002;
TcpServer tcpServer = new TcpServer(port);
tcpServer.start();
}
}
Tcp client
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner; public class TcpClient {
private Socket socket;
private int port; private TcpClient(int port){
// try {
this.port = port;
// } catch (IOException e) {
// e.printStackTrace();
// }
} /**
* 发送消息
* @param message
*/
public void sendMessage(String message){
try {
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), port);
OutputStream os = socket.getOutputStream();
byte buff[] = message.getBytes();
os.write(buff);
os.flush();
socket.close();
} catch (IOException e) {
e.printStackTrace();
} } public void inputMessage(){
Scanner scanner = new Scanner(System.in);
while(true){
String message = scanner.nextLine();
sendMessage(message);
}
} public static void main(String[] args) {
int port = 10001;
TcpClient client = new TcpClient(port);
client.inputMessage();
} }