新手一枚 不好别喷的太严重了
建立 服务端 :
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/**
*
* @author bigSea
* Socket服务端
*/
public class MyTcpService {
public static void main(String[] args) {
System.out.println("主机启动。。。");
try {
//创建服务器,并开放10000端口
ServerSocket server = new ServerSocket(10000);
while(true){
//从连接队列中取出一个连接,如果没有则等待
Socket socket = server.accept();
//启动线程
new Thread(new ToClientDialog(socket)).start();
System.out.println("客户端连接成功 地址:"+socket.getInetAddress()+" 端口:"+socket.getPort());//接收和发送数据
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
ToClientDialog类:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
*
* @author bigSea
* 向客户端发送数据
*/
public class ToClientDialog implements Runnable{
private Socket dialogSocket = null;
public ToClientDialog() {
super();
}
//初始化
public ToClientDialog(Socket socket){
dialogSocket = socket;
}
public void run() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if (dialogSocket != null) {
BufferedReader in;
PrintWriter out;
try {
//接收
in = new BufferedReader(new InputStreamReader(dialogSocket.getInputStream()));
//输出
out = new PrintWriter(dialogSocket.getOutputStream(), true);
//获取用户输入
BufferedReader line = new BufferedReader(new InputStreamReader(
System.in));
while(true){
//接收客户端发送来的数据
String msg = in.readLine();
if (msg.equals("end")) {
break;
}
System.out.println(sdf.format(new Date()) + " client:" + msg);
//向客户端发送用户输入的数据
out.println(line.readLine());
}
line.close();
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
客户端代码:
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
/**
* @author bigSea
* Socket 服务端
*/
public class MyTcpClient {
public static void main(String[] args) {
System.out.println("客户机启动。。。");
Socket socket;
try {
//连接主机地址。
socket = new Socket("192.168.72.175", 10000);
//启动
new Thread(new ToServiceDialog(socket)).start();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
ToServiceDialog代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
*
* @author bigSea
* 向服务端发送信息
*/
public class ToServiceDialog implements Runnable {
private Socket dialogSocket = null;
public ToServiceDialog(){
}
//初始化
public ToServiceDialog(Socket socket){
dialogSocket = socket;
System.out.println("连接"+socket.getInetAddress().getHostName()+"主机成功");
}
public void run() {
BufferedReader in;
PrintWriter out;
BufferedReader line;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if (dialogSocket != null) {
try {
//接收
in = new BufferedReader(new InputStreamReader(
dialogSocket.getInputStream()));
//输出
out = new PrintWriter(dialogSocket.getOutputStream(), true);
//获取用户输入
line = new BufferedReader(new InputStreamReader(
System.in));
while(true){
//接受控制台输入的数据
String sendMsg = line.readLine();
//向服务端发送数据
out.println(sendMsg);
if (sendMsg.equals("end")) {
break;
}
//接收服务端发送来的数据
String msg = in.readLine();
System.out.println(sdf.format(new Date()) + " service:" + msg);
}
line.close();
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
演示效果: