小动手
TCP
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class Client {
public static void main(String[] args) {
String host = "127.0.0.1";
int port = 6789;
Socket socket = null;
try {
socket = new Socket(host, port);
Thread sendThread = new Thread(new sendHandleThread(socket));
Thread serverThread = new Thread(new serverHandleThread(socket));
sendThread.start();
serverThread.start();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
}
class serverHandleThread implements Runnable {
private Socket socket;
public serverHandleThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
String line = null;
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while ((line = bufferedReader.readLine())!=null) {
System.out.println("server:" + line);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
bufferedReader.close();
if (socket != null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class sendHandleThread implements Runnable {
private Socket socket;
public sendHandleThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
String line = null;
BufferedReader bufferedReader = null;
PrintWriter printWriter = null;
try {
printWriter = new PrintWriter(this.socket.getOutputStream(), true);
bufferedReader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
line = bufferedReader.readLine();
System.out.println(line);
printWriter.println(line);
if (line.equals("exit")) {
break;
}
//printWriter.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (printWriter != null) {
printWriter.close();
}
try {
//bufferedReader.close();
if (socket != null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
/**
* Created by liqiushi on 2017/11/19.
*/
public class Server {
public static void main(String[] args) {
ServerSocket serverSocket = null;
int port = 6789;
try {
serverSocket = new ServerSocket(port);
while (true) {
Socket socket = serverSocket.accept();
Thread serverThread = new Thread(new serverHandleThread(socket));
Thread sendThread = new Thread(new sendHandleThread(socket));
serverThread.start();
sendThread.start();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
System.out.println("server exit");
try {
if (serverSocket != null) {
serverSocket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class serverHandleThread implements Runnable {
private Socket socket;
public serverHandleThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
String line = null;
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println(socket.getInetAddress() + ":" + socket.getPort() + "进来了");
while ((line = bufferedReader.readLine())!=null) {
System.out.println("client:" + line);
if (line.equals("exit")) {
bufferedReader.close();
socket.close();
}
}
} catch (IOException e) {
System.out.println("对方异常退出!");
e.printStackTrace();
}
}
}
class sendHandleThread implements Runnable {
private Socket socket;
public sendHandleThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
String line = null;
BufferedReader bufferedReader = null;
try {
PrintWriter printWriter = new PrintWriter(socket.getOutputStream(),true);
bufferedReader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
line = bufferedReader.readLine();
System.out.println(line);
printWriter.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
UDP
import com.sun.xml.internal.messaging.saaj.util.ByteInputStream;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class UDPServer {
public static void main(String[] args) {
try {
DatagramSocket socket = new DatagramSocket(5678);
byte[] recvBuf = new byte[1024];
DatagramPacket recvPacket = new DatagramPacket(recvBuf,recvBuf.length);
try {
while(true){
socket.receive(recvPacket);
ByteArrayInputStream bis = new ByteArrayInputStream(recvBuf);
DataInputStream dis = new DataInputStream(bis);
System.out.println(dis.readLong());
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (SocketException e) {
e.printStackTrace();
}
}
}
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
/**
* UDP传输一个long型数据
* Created by liqiushi on 2017/11/21.
*/
public class UDPclient {
public static void main(String[] args) {
long sendNum = 10000L;
ByteArrayOutputStream bao = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bao);
try {
dos.writeLong(sendNum);
} catch (IOException e) {
e.printStackTrace();
}
byte[] sendBuf = bao.toByteArray();
//UDP是无连接的,但是需要指定目的地址和端口,网络层才能进行路由转发
DatagramPacket sendPacket = new DatagramPacket(sendBuf,0,sendBuf.length,new InetSocketAddress("127.0.0.1",5678));
try {
//创建监听端口
DatagramSocket socket = new DatagramSocket(9999);
socket.send(sendPacket);
socket.close();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
}
}
}
其他
windows下关闭占用端口的进程
- 查看端口
netstat -ano | findstr <port>
- kill
taskkill /F /PID <PID>