import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
public class TCPSocketClient {
public static void main(String[] args) { // java程序主入口处
try {
new Client("localhost", 8080); // IP地址为本机,端口为80
} catch (Exception e) { // 捕获异常
System.out.println("测试客户端连接出错:" + e.getMessage());
}
}
}
class Client { // Socket客户端
private String host; // IP地址(域名)
private int port; // 端口号
public Client(String host, int port) { // 带参数构造方法进行初始化
this.host = host;
this.port = port;
connectServer(); // 调用连接方法
}
public void connectServer() { // 连接方法
try {
Socket socketConn; // 声明Socket连接
// 判断IP地址(域名)如果是本机localhost
if (host.equals("localhost") || host.equals("127.0.0.1")) {
// 创建本地Socket连接
socketConn = new Socket(InetAddress.getLocalHost(), port);
} else { // 创建远程Socket连接
socketConn = new Socket(InetAddress.getByName(host), port);
}
BufferedReader stdin = new BufferedReader(new InputStreamReader(
System.in)); // 获得从键盘输入的流
PrintWriter out = new PrintWriter(socketConn.getOutputStream(),
true); // 获得服务器写内容的数据流
BufferedReader in = new BufferedReader(new InputStreamReader(
socketConn.getInputStream()));// 获得接收服务器发送内容的缓冲流
// 从服务器获得信息
System.out.println("服务器信息:" + in.readLine());
System.out.println("服务器信息:" + in.readLine());
System.out.print("请输入>"); // 用户输入
boolean done = false;
while (!done) {
String line = stdin.readLine(); // 获得从键盘输入的每行字符
out.println(line); // 发送到服务端
if (line.equalsIgnoreCase("quit")) // 读到quit则结束循环
done = true;
String info = in.readLine(); // 从服务器读取字符串
System.out.println("服务器信息:" + info); // 显示从服务器发送来的数据
if (!done) // 用户输入
System.out.print("请输入>");
}
socketConn.close(); // 关闭资源
} catch (SecurityException e) { // 捕获安全性错误时引发的异常
System.out.println("连接服务器出现安全问题!");
} catch (IOException e) { // 捕获IO流异常
System.out.println("连接服务器出现I/O错误!");
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
java TCP协议客户机.zip (2个子文件)
Java.jpg 77KB
TCPSocketClient.java 2KB
共 2 条
- 1
资源评论
- ylB090820412012-05-27可以实现通信,不过没有窗口界面。
lihaiyang68
- 粉丝: 2
- 资源: 51
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功