//简单的JAVA多线程 TCP服务器
package net;
import java.io.*;import java.net.*;
public class MulThreadSocketServer {
public static void main(String[] args){
ServerSocket ss = null;
Socket socket = null;
final int port = 9002;
try {
ss = new ServerSocket(port);
//服务器已启动
while(true){
//获得连接
socket = ss.accept();
//创建线程
new LogicThread(socket);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
//关闭连接
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class LogicThread extends Thread{
Socket socket;
InputStream is;
OutputStream os;
public LogicThread(Socket socket){
this.socket = socket;
start();
}
public void run(){
byte[] b = new byte[1024];
try {
os = socket.getOutputStream();
is = socket.getInputStream();
for (int i = 0; i < 5; i++) {
int n = is.read(b);
System.out.println(new String(b, 0, n));
os.write(b, 0, n);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
os.close();
is.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//简单的JAVA TCP客户端
package net;
import java.net.*;
import java.io.*;
public class SocketClient {
public static void main(String[] args){
Socket socket = null;
InputStream is = null;
OutputStream os = null;
String serverIp = "127.0.0.1";
final int port = 9002;
String data = "hello world";
try {
socket = new Socket(serverIp,port);
is = socket.getInputStream();
os = socket.getOutputStream();
byte[] b = new byte[1024];
for(int i=0;i<5;i++){
//发送数据
os.write(data.getBytes());
//接收数据
int n = is.read(b);
System.out.println(new String(b,0,n));
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
is.close();
os.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//简单的JAVA 多线程UDP服务器
package net;
import java.net.*;
public class MulThreadUDPServer {
public static void main(String[] args){
DatagramSocket ds = null;
DatagramPacket receiveDp = null;
final int port = 9000;
byte[] b = new byte[1024];
receiveDp = new DatagramPacket(b,b.length);
try {
ds = new DatagramSocket(port);
System.out.println("服务器已启动");
//服务器已启动
while(true){
ds.receive(receiveDp);
new LogicThread2(ds,receiveDp);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
ds.close();
}
}
}
class LogicThread2 extends Thread{
DatagramSocket ds;
DatagramPacket receiveDp;
public LogicThread2(DatagramSocket ds, DatagramPacket receiveDp) {
super();
this.ds = ds;
this.receiveDp = receiveDp;
start();
}
public void run(){
try {
byte[] b = receiveDp.getData();
int len = receiveDp.getLength();
int clientPort = receiveDp.getPort();
InetAddress clientIp = receiveDp.getAddress();
System.out.println("client ip:"+clientIp);
System.out.println("port:"+clientPort);
System.out.println("data:"+new String(b,0,len));
byte[] data = "OK".getBytes();
DatagramPacket sendDp = new DatagramPacket(data,data.length,clientIp,clientPort);
ds.send(sendDp);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//简单的UDP客户端
package net;
import java.net.*;
//import java.util.*;
public class SimpleUDPClient {
public static void main(String[] args){
DatagramSocket ds = null;
DatagramPacket sendDp = null;
DatagramPacket receiveDp = null;
String serverIp = "127.0.0.1";
final int port = 9000;
try {
ds = new DatagramSocket();
//Date d = new Date();
String dString = "hello world";
byte[] data = dString.getBytes();
InetAddress address = InetAddress.getByName(serverIp);
sendDp = new DatagramPacket(data,data.length,address,port);
ds.send(sendDp);
System.out.println("send:"+dString);
byte[] receiveD = new byte[1024];
receiveDp = new DatagramPacket(receiveD,receiveD.length);
ds.receive(receiveDp);
byte[] response = receiveDp.getData();
int len = receiveDp.getLength();
String recString = new String(response,0,len);
System.out.println("receive:"+recString);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
ds.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}