Socket网络编程简单实例Demo,文件传输。
2017-08-28 18:37
399 查看
tcp/ip传输协议方式:
package com.text.sgl;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
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 org.junit.Test;
/**
* Socket编程简单demo
* @author shen.guoliang
* @version 1.0, 2017年8月28日 每次修改后更新版本号,日期和修改内容
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class SocketTest {
//客户端
@Test
public void client(){
Socket s=null;
OutputStream out=null;
FileInputStream pic=null;
InputStream i=null;
BufferedInputStream bfi=null;
try {
//生成socket链接
s = new Socket(InetAddress.getByName("127.0.0.1"), 9999);
//通过socket生成输出到服务器端的输出流
out = s.getOutputStream();
//获取客户端项目路径文件psb
pic = new FileInputStream (new File("psb.png"));
//使用缓冲流包装提速
bfi= new BufferedInputStream(pic);
byte [] b = new byte[1024];
int len;
while((len = bfi.read(b))!=-1){
//将psb文件写出到输出流里面
out.write(b, 0, len);
}
//应为客户端与服务器端用的原始InputStream和OutputStream传输的,具有阻塞性
//所以上面文件输出完了需要,告诉socket关闭output方便后面接收服务器端传回的消息
s.shutdownOutput();
//使用socket接收服务器端返回的消息输出
i = s.getInputStream();
byte [] b2= new byte[1024];
int len1;
while((len1=i.read(b2))!=-1){
System.out.println(new String(b2,0, len1));
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(bfi!=null){
try {
bfi.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(i!=null){
try {
i.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(pic!=null){
try {
pic.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(s!=null){
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//服务端
@Test
public void server(){
ServerSocket ss=null;
Socket s=null;
InputStream is=null;
FileOutputStream out=null;
BufferedOutputStream bout=null;
OutputStream o=null;
try {
//服务器端通过相同的端口号创建serversocket对象
ss = new ServerSocket(9999);
//通过serversocket对象得到socket对象
s = ss.accept();
//通过socket获取客户端发送的流
is = s.getInputStream();
out = new FileOutputStream("psb2.png");
bout = new BufferedOutputStream(out);
byte [] b = new byte[1024];
int len;
//读取客户端发送的流
while((len=is.read(b))!=-1){
//写入服务器端文件系统
bout.write(b, 0, len);
}
//返回消息给客户端
o = s.getOutputStream();
o.write("我已经接收到图片了".getBytes());
} catch (IOException e) {
e.printStackTrace();
}finally {
if(o!=null){
try {
o.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bout!=null){
try {
bout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
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();
}
}
}
}
}
注意:
1:inputstream和outputstream存在阻塞问题。需要使用shutdownOutput()方法,提示服务端本次传输完成
2:注意资源,finally里面关闭所有资源
udp传输协议方式:
/**
* udp传输协议测试
*
* @author shen.guoliang
* @version 1.0, 2017年8月28日 每次修改后更新版本号,日期和修改内容
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class UdpTest {
// 发送端
@Test
public void send() {
DatagramSocket ds = null;
try {
ds = new DatagramSocket();
byte[] b = "你好,这是我发送的数据".getBytes();
//创建一个数据报,每个数据报不大于64K,都记录这数据信息,发送端的ip,端口号,以及要发送到的接收端的ip,端口号
DatagramPacket dp = new DatagramPacket(b, 0, b.length, InetAddress.getByName("127.0.0.1"), 9999);
//使用DatagramSocket发数据报发送出去
ds.send(dp);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ds != null) {
ds.close();
}
}
}
// 接收端
@Test
public void rceive() {
DatagramSocket ds = null;
try {
//使用相同的端口接收信息
ds = new DatagramSocket(9999);
//创建字节数组接收数据
byte [] b = new byte[1024];
DatagramPacket dp = new DatagramPacket(b, 0, b.length);
ds.receive(dp);
System.out.println(new String(b));
}catch (IOException e) {
e.printStackTrace();
}finally {
if(ds!=null){
ds.close();
}
}
}
}
package com.text.sgl;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
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 org.junit.Test;
/**
* Socket编程简单demo
* @author shen.guoliang
* @version 1.0, 2017年8月28日 每次修改后更新版本号,日期和修改内容
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class SocketTest {
//客户端
@Test
public void client(){
Socket s=null;
OutputStream out=null;
FileInputStream pic=null;
InputStream i=null;
BufferedInputStream bfi=null;
try {
//生成socket链接
s = new Socket(InetAddress.getByName("127.0.0.1"), 9999);
//通过socket生成输出到服务器端的输出流
out = s.getOutputStream();
//获取客户端项目路径文件psb
pic = new FileInputStream (new File("psb.png"));
//使用缓冲流包装提速
bfi= new BufferedInputStream(pic);
byte [] b = new byte[1024];
int len;
while((len = bfi.read(b))!=-1){
//将psb文件写出到输出流里面
out.write(b, 0, len);
}
//应为客户端与服务器端用的原始InputStream和OutputStream传输的,具有阻塞性
//所以上面文件输出完了需要,告诉socket关闭output方便后面接收服务器端传回的消息
s.shutdownOutput();
//使用socket接收服务器端返回的消息输出
i = s.getInputStream();
byte [] b2= new byte[1024];
int len1;
while((len1=i.read(b2))!=-1){
System.out.println(new String(b2,0, len1));
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(bfi!=null){
try {
bfi.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(i!=null){
try {
i.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(pic!=null){
try {
pic.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(s!=null){
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//服务端
@Test
public void server(){
ServerSocket ss=null;
Socket s=null;
InputStream is=null;
FileOutputStream out=null;
BufferedOutputStream bout=null;
OutputStream o=null;
try {
//服务器端通过相同的端口号创建serversocket对象
ss = new ServerSocket(9999);
//通过serversocket对象得到socket对象
s = ss.accept();
//通过socket获取客户端发送的流
is = s.getInputStream();
out = new FileOutputStream("psb2.png");
bout = new BufferedOutputStream(out);
byte [] b = new byte[1024];
int len;
//读取客户端发送的流
while((len=is.read(b))!=-1){
//写入服务器端文件系统
bout.write(b, 0, len);
}
//返回消息给客户端
o = s.getOutputStream();
o.write("我已经接收到图片了".getBytes());
} catch (IOException e) {
e.printStackTrace();
}finally {
if(o!=null){
try {
o.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bout!=null){
try {
bout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
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();
}
}
}
}
}
注意:
1:inputstream和outputstream存在阻塞问题。需要使用shutdownOutput()方法,提示服务端本次传输完成
2:注意资源,finally里面关闭所有资源
udp传输协议方式:
/**
* udp传输协议测试
*
* @author shen.guoliang
* @version 1.0, 2017年8月28日 每次修改后更新版本号,日期和修改内容
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class UdpTest {
// 发送端
@Test
public void send() {
DatagramSocket ds = null;
try {
ds = new DatagramSocket();
byte[] b = "你好,这是我发送的数据".getBytes();
//创建一个数据报,每个数据报不大于64K,都记录这数据信息,发送端的ip,端口号,以及要发送到的接收端的ip,端口号
DatagramPacket dp = new DatagramPacket(b, 0, b.length, InetAddress.getByName("127.0.0.1"), 9999);
//使用DatagramSocket发数据报发送出去
ds.send(dp);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ds != null) {
ds.close();
}
}
}
// 接收端
@Test
public void rceive() {
DatagramSocket ds = null;
try {
//使用相同的端口接收信息
ds = new DatagramSocket(9999);
//创建字节数组接收数据
byte [] b = new byte[1024];
DatagramPacket dp = new DatagramPacket(b, 0, b.length);
ds.receive(dp);
System.out.println(new String(b));
}catch (IOException e) {
e.printStackTrace();
}finally {
if(ds!=null){
ds.close();
}
}
}
}
相关文章推荐
- linux C 简单socket网络编程的实例
- 高并发MINA框架,网络编程(SOCKET)实现,简单的网络聊天DEMO
- PHP异步调用socket简单实例
- 一个简单的socket监听的服务器实例
- 桌面邮件飞机源码——VB.NET网络编程简单实例
- 非阻塞socket下客户端connect + select简单demo
- linq to access 简单实现 实例demo
- C# socket 多线程多客户端简单实例
- 循序渐进Socket网络编程(多客户端、信息共享、文件传输)
- java websocket简单实例绝对可用
- Linux C Socket编程原理及简单实例
- python网络编程中socket、select和poll的简单使用
- Linux C 网络编程——4. 简单TCP Socket程序
- Socket网络编程 -iOS开发简单实例
- SOCKET简单实例
- Socket编程java[简单易懂],附带Android Studio 项目聊天实例
- Win32 socket简单实例
- java+html5的websocket简单实现BS聊天DEMO实例
- socket.io简单说明及在线抽奖demo
- Linux C Socket编程原理及简单实例