TCP编程1:客户端和服务器端的创建
2015-05-08 14:23
423 查看
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import org.junit.Test;
public class TestTCP1 {
/*
* 客户端
*/
@Test
public void client() {
OutputStream os = null;
try {
// 1.创建一个Socket的对象,通过构造器指明服务器端的IP地址,以及其接收程序的端口号
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090);
// 2.getOutputStream():发送数据,方法返回OutputStream的对象
os = socket.getOutputStream();
// 3.具体的输出过程
os.write("我是服务器端".getBytes());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.关闭流
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/*
* 服务器端
*/
@Test
public void server() {
ServerSocket ss = null;
Socket s = null;
InputStream is = null;
try {
//1.创建一个ServerSocket的对象,通过构造器指明自身的端口号
ss = new ServerSocket(9090);
//2.调用accept()方法,返回一个Socket的对象
s = ss.accept();
//3.调用Socket对象的getInputStream()方法获取从客户端发送过来的输入流
is = s.getInputStream();
//4.对获取的流进行操作
byte[] b = new byte[1024];
int len;
while ((len = is.read(b)) != -1) {
String str = new String(b, 0, len);
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//5.关闭流以及Socket、ServerSocket的对象
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (s != null) {
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (ss != null) {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import org.junit.Test;
public class TestTCP1 {
/*
* 客户端
*/
@Test
public void client() {
OutputStream os = null;
try {
// 1.创建一个Socket的对象,通过构造器指明服务器端的IP地址,以及其接收程序的端口号
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090);
// 2.getOutputStream():发送数据,方法返回OutputStream的对象
os = socket.getOutputStream();
// 3.具体的输出过程
os.write("我是服务器端".getBytes());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.关闭流
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/*
* 服务器端
*/
@Test
public void server() {
ServerSocket ss = null;
Socket s = null;
InputStream is = null;
try {
//1.创建一个ServerSocket的对象,通过构造器指明自身的端口号
ss = new ServerSocket(9090);
//2.调用accept()方法,返回一个Socket的对象
s = ss.accept();
//3.调用Socket对象的getInputStream()方法获取从客户端发送过来的输入流
is = s.getInputStream();
//4.对获取的流进行操作
byte[] b = new byte[1024];
int len;
while ((len = is.read(b)) != -1) {
String str = new String(b, 0, len);
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//5.关闭流以及Socket、ServerSocket的对象
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (s != null) {
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (ss != null) {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
相关文章推荐
- JAVA网络编程-TCP客户端与服务器端连接
- 基于TCP/IP的套接字服务器端和客户端编程
- Pythoh网络编程3:创建TCP服务器和客户端
- Linux socket编程入门及客户端服务器端通信实现 – 提高篇:TCP连接过程分析
- 基于TCP的服务器端/客户端(二)---------网络编程(Linux----C)
- 基于C#的socket编程的TCP异步实现 ,包含服务器端与客户端源代码
- python 网络编程(三)---TCP 服务器端客户端实现
- 黑马程序员--10.网络编程--05.【TCP协议相关类】【InetSocketAddress类】【Socket类】【ServerSocket类】【TCP客户端和服务器端】
- 基于TCP的客户端、服务器端socket编程
- 关于Python基于TCP的服务器端和客户端编程
- TCP,UDP客户端和服务器端编程的一般步骤
- 网络编程Tcp的客户端和服务器端
- 牛客网Java刷题知识点之TCP、UDP、TCP和UDP的区别、socket、TCP编程的客户端一般步骤、TCP编程的服务器端一般步骤、UDP编程的客户端一般步骤、UDP编程的服务器端一般步骤
- linux网络编程,将服务器端时间通过tcp传送到客户端
- 网络编程-TCP服务器端程序和客户端程序
- TCP/IP网络编程_echo回声服务器端与/客户端
- (第三季)601-socket编程-tcp服务器端;602-socket编程-tcp客户端
- TCP编程例三:从客户端发送文件给服务器端,服务器端保存到本地,并返回“发送成功”给客户端。
- TCP编程2: 客户端给服务器端发信息,服务器端将信息打印到控制台上,同时发送"已收到信息"给客户端
- 基于TCP的服务器端/客户端(一)---------网络编程(linux----C)