package myNetDemo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
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;
//TCP编程:从客户端发送文件给服务端,服务端保存到本地,并返回“发送成功”给客户端,并关闭相应的连接
//如下程序要处理异常时,使用try-catch-finally
public class TestTCP {
@Test
public void client() {
// 1.创建socket对象
Socket socket = null;
// 2.从本地用文件流形式把文件读进来
FileInputStream fis = null;
// 3.写出去用socket形式写出去
OutputStream os = null;
// 4接受服务端发来的信息
InputStream is = null;
try {
socket = new Socket(InetAddress.getByName("127.0.0.1"), 9898);
fis = new FileInputStream(new File("1.png"));
os = socket.getOutputStream();
byte[] b = new byte[1024];
int len;
while ((len = fis.read(b)) != -1) {
os.write(b, 0, len);// 写出去,在用服务端接受
}
//用标识标志客户端发完了
socket.shutdownOutput();
is = socket.getInputStream();
byte[] b1 = new byte[1024];
int len1;
while ((len1 = is.read(b1)) != -1) {
// 5输出到控制台上
String str = new String(b1, 0, len1);
System.out.println(str);
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (socket != null) {
// 6.关闭
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
@Test
public void service() {
// 1.创建一个ServerSocket对象
ServerSocket ss = null;
// 2.调用accept的方法返回socket对象
Socket s = null;
// 3。将从客户端发送来的信息,在写入本地
InputStream is = null;
// 4.输出到本地
FileOutputStream fos = null;
// 5.发送信息反馈给客户端
OutputStream os = null;
try {
ss = new ServerSocket(9898);
s = ss.accept();
is = s.getInputStream();
fos = new FileOutputStream(new File("2.png"));
byte[] b = new byte[1024];
int len;
while ((len = is.read(b)) != -1) {
fos.write(b, 0, len);// 接受并输出到本地
}
System.out.println("收到来自"+s.getInetAddress().getHostAddress()+"的图片");
os = s.getOutputStream();
os.write("你发送的图片我已经接收成功!".getBytes());// 客户端接受
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (os != null) {
// 6.关闭
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (s != null) {
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (ss != null) {
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
TCP网络编程demo
最新推荐文章于 2023-08-12 14:30:43 发布