TCP网络编程例子
Posted kukai
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TCP网络编程例子相关的知识,希望对你有一定的参考价值。
//客户端
package socket;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class Client001 {
public void client() {
Socket socket = null;
OutputStream os = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
//创建IP对象,指明服务器端的IP地址
InetAddress inet = InetAddress.getByName("127.0.0.1");
//1.创建Socket对象,指明服务器的ip和端口号
socket = new Socket(inet, 43333);
//2.获取一个输出流,用于输出数据
os = socket.getOutputStream();
//3.写出数据操作
os.write("你好,我是客户端".getBytes());
//关闭数据输出流
socket.shutdownOutput();
is = socket.getInputStream();
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[20];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
baos.write(buffer, 0, len1);
}
System.out.println(baos.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.资源关闭
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (baos != null) {
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
Client001 tcp = new Client001();
tcp.client();
}
}
//服务端
package socket;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class ServiceDem001 {
public void server() {
ServerSocket ss = null;
Socket socket = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
OutputStream os = null;
try {
//1.创建服务器端的ServeSocket,指明自己的端口号
ss = new ServerSocket(43333);
//2.调用accept()表示接受来自于客户端的Socket;
socket = ss.accept();
//3.创建一个输入流
is = socket.getInputStream();
//读取输入流中的数据
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[20];
int len;
while ((len = is.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
System.out.println(baos.toString());
System.out.println("收到了来自于:" + socket.getInetAddress().getHostName());
os = socket.getOutputStream();
os.write("你好,已收到你的信息".getBytes());
} catch (IOException e)
{
e.printStackTrace();
} finally
{
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
if (baos != null) {
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (ss != null) {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
public static void main(String[] args) {
ServiceDem001 service = new ServiceDem001();
service.server();
}
}
以上是关于TCP网络编程例子的主要内容,如果未能解决你的问题,请参考以下文章
boost::asio::ip::tcp实现网络通信的小例子