TCP案例代码
Posted 技术很low的瓜贼
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TCP案例代码相关的知识,希望对你有一定的参考价值。
文章目录
TCP网络编程的案例代码
描述客户端与服务端的通信
普通服务端/客户端代码
- 服务端
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
/**
* 服务端
*/
public class ServerSocketTest
public static void main(String[] args)
ServerSocket ss = null;
Socket s = null;
// BufferedInputStream bis = null;
BufferedReader br = null;
PrintStream ps = null;
try
// 创建ServerSocket类型的对象并提供端口号
ss = new ServerSocket(8888);
System.out.println("正在等待连接");
// 等待客户端的链接请求 调用accept方法
s = ss.accept();
System.out.println("客户端链接成功");
// 使用输入输出流进行通信
// bis = new BufferedInputStream(s.getInputStream()); // 获取汉字出现乱码
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
ps = new PrintStream(s.getOutputStream());
while (true)
System.out.println("获取到服务端的内容为");
String s1 = br.readLine();
System.out.println(s1); // 此处使用read()会使得回车无法解析,无法终止
System.out.println("接收成功");
System.out.println("服务器给客户端发送消息");
ps.println("服务端收到!!!!");
if (s1.equals("bye"))
break;
catch (IOException e)
e.printStackTrace();
finally
if (null != ps)
ps.close();
// 关闭Socket并释放有关资源
if (null != s)
try
s.close();
catch (IOException e)
e.printStackTrace();
if (null != ss)
try
ss.close();
catch (IOException e)
e.printStackTrace();
if (null != br)
try
br.close();
catch (IOException e)
e.printStackTrace();
- 客户端
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;
/**
* 客户端
*/
public class SocketTest
public static void main(String[] args)
// 创建Socket类型的对象并提供服务端的主机名和端口号
Socket s = null;
PrintStream ps = null;
Scanner sc = null;
BufferedReader br = null;
try
s = new Socket("Myd0130", 8888);
System.out.println("链接服务器成功");
Thread.sleep(5000);
sc = new Scanner(System.in);
// 使用输入输出流进行通信
// 客户端向服务端发送消息
ps = new PrintStream(s.getOutputStream());
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
while (true)
System.out.println("请输入要发送内容");
String str = sc.next();
ps.println(str);
if (str.equals("bye"))
break;
System.out.println("发送内容成功");
System.out.println("接收到的消息为");
String s1 = br.readLine();
System.out.println(s1);
catch (IOException | InterruptedException e)
e.printStackTrace();
finally
if (null != br)
try
br.close();
catch (IOException e)
e.printStackTrace();
if (null != s)
try
// 关闭Socket对象,并释放相关资源
s.close();
catch (IOException e)
e.printStackTrace();
if (null != sc)
sc.close();
if (null != ps)
ps.close();
多线程方式代码
- 多线程
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
/**
* 服务端
*/
public class ThreadsSocket implements Runnable
private Socket s;
public ThreadsSocket(Socket s)
this.s = s;
@Override
public void run()
BufferedReader br = null;
PrintStream ps = null;
try
// 3.使用输入输出流进行通信
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
ps = new PrintStream(s.getOutputStream());
while(true)
// 实现对客户端发来字符串内容的接收并打印
// 当没有数据发来时,下面的方法会形成阻塞
String s1 = br.readLine();
InetAddress inetAddress = s.getInetAddress();
System.out.println("客户端" + inetAddress + "发来的字符串内容是:" + s1);
// 当客户端发来的内容为"bye"时,则聊天结束
if ("bye".equalsIgnoreCase(s1))
System.out.println("客户端" + inetAddress + "已下线!");
break;
// 实现服务器向客户端回发字符串内容"I received!"
ps.println("I received!");
System.out.println("服务器发送数据成功!");
catch (IOException e)
e.printStackTrace();
finally
if (null != ps)
ps.close();
if (null != br)
try
br.close();
catch (IOException e)
e.printStackTrace();
if (null != s)
try
s.close();
catch (IOException e)
e.printStackTrace();
- 服务端
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* 服务端
*/
public class StartTest
public static void main(String[] args)
ServerSocket ss = null;
Socket s = null;
try
// 创建ServerSocket类型的对象并提供端口号
ss = new ServerSocket(8888);
System.out.println("正在等待连接");
// 等待客户端的链接请求 调用accept方法
System.out.println("等待连接请求");
while (true)
s = ss.accept();
System.out.println("客户端链接成功");
ThreadsSocket threadsSocket = new ThreadsSocket(s);
Thread t = new Thread(threadsSocket);
t.start();
catch (IOException e)
e.printStackTrace();
finally
// 关闭Socket并释放有关资源
if (null != s)
try
s.close();
catch (IOException e)
e.printStackTrace();
if (null != ss)
try
ss.close();
catch (IOException e)
e.printStackTrace();
- 客户端(多复制几份即可描述多个客户端)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;
/**
* 客户端
*/
public class SocketTest1
public static void main(String[] args)
// 创建Socket类型的对象并提供服务端的主机名和端口号
Socket s = null;
PrintStream ps = null;
Scanner sc = null;
BufferedReader br = null;
try
s = new Socket("Myd0130", 8888);
InetAddress inetAddress = s.getInetAddress();
System.out.println(inetAddress + "连接服务器成功");
Thread.sleep(5000);
sc = new Scanner(System.in);
// 使用输入输出流进行通信
// 客户端向服务端发送消息
ps = new PrintStream(s.getOutputStream());
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
while (true)
System.out.println("请输入要发送内容");
String str = sc.next();
ps.println(str);
if (str.equals("bye"))
break;
System.out.println("发送内容成功");
System.out.println("接收到的消息为");
String s1 = br.readLine();
System.out.println(s1);
catch (IOException | InterruptedException e)
e.printStackTrace();
finally
if (null != br)
try
br.close();
catch (IOException e)
e.printStackTrace();
if (null != s)
try
// 关闭Socket对象,并释放相关资源
s.close();
catch (IOException e)
e.printStackTrace();
if (null != sc)
sc.close();
if (null != ps)
ps.close();
以上是关于TCP案例代码的主要内容,如果未能解决你的问题,请参考以下文章