目录

字符传输案例

服务端

客户端

写入文本文件案例

服务端

客户端

图片传输案例

服务端

客户端


字符传输案例

服务端

package com.ctx.tcp2;import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;/*** 服务端接受消息* * @author ashen**/
public class TCPService {public static void main(String[] args) {new TCPService().service();}public void service() {ServerSocket ss = null;Socket socket = null;InputStream is = null;ByteArrayOutputStream baos = null;try {ss = new ServerSocket(4399);socket = ss.accept();is = socket.getInputStream();baos = new ByteArrayOutputStream();byte[] buffer = new byte[1];int len;while ((len = is.read(buffer)) != -1) {baos.write(buffer, 0, len);}System.out.println(baos.toString());} catch (Exception e) {} finally {if (baos != null) {try {baos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if (is != null) {try {is.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if (socket != null) {try {socket.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if (ss != null) {try {ss.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
}

客户端

package com.ctx.tcp2;import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;/*** 客户端发送消息* * @author ashen**/
public class TCPClient {public static void main(String[] args) {new TCPClient().client();}public void client() {Socket socket = null;OutputStream os = null;try {InetAddress inetAddress = InetAddress.getByName("192.168.0.107");socket = new Socket(inetAddress, 4399);os = socket.getOutputStream();os.write("始于颜值,终于才华。".getBytes());} catch (Exception e) {} finally {if (os != null) {try {os.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if (socket != null) {try {socket.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}}

写入文本文件案例

服务端

package com.ctx.tcp;import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;/*** 服务端接收消息并保存成txt文件* * @author ashen**/
public class TCPService {public static void main(String[] args) {new TCPService().service();}public void service() {ServerSocket ss = null;FileOutputStream fos = null;InputStream is = null;Socket socket = null;try {ss = new ServerSocket(4399);socket = ss.accept();is = socket.getInputStream();fos = new FileOutputStream(new File("D:/images/a.txt"));byte[] buffer = new byte[50000];int len = 0;while ((len = is.read(buffer)) != -1) {fos.write(buffer, 0, len);}OutputStream os = socket.getOutputStream();os.write("服务端接受完毕。".getBytes());} catch (Exception e) {} finally {if (fos != null) {try {fos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if (is != null) {try {is.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if (socket != null) {try {socket.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if (ss != null) {try {ss.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
}

客户端

package com.ctx.tcp;import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;/*** 客户端发送消息* * @author ashen**/
public class TCPClient {public static void main(String[] args) {new TCPClient().client();}public void client() {Socket socket = null;OutputStream os = null;try {InetAddress inetAddress = InetAddress.getByName("192.168.0.107");socket = new Socket(inetAddress, 4399);os = socket.getOutputStream();os.write("始于颜值,终于才华。".getBytes());} catch (Exception e) {} finally {if(os!=null) {try {os.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(socket!=null) {try {socket.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}}

图片传输案例

服务端

package com.ctx.tcp3;import java.io.ByteArrayOutputStream;
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.ServerSocket;
import java.net.Socket;/*** 服务端接收图片* * @author ashen**/
public class TCPService {public static void main(String[] args) {new TCPService().service();}public void service() {ServerSocket ss = null;Socket socket = null;InputStream is = null;FileOutputStream fos = null;OutputStream os = null;try {ss = new ServerSocket(4399);socket = ss.accept();is = socket.getInputStream();fos = new FileOutputStream(new File("D:/images/a.jpg"));byte[] buffer = new byte[50000];int len = 0;while ((len = is.read(buffer)) != -1) {fos.write(buffer, 0, len);}os = socket.getOutputStream();os.write("服务端接受完毕。".getBytes());} catch (Exception e) {} finally {if (fos != null) {try {fos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if (is != null) {try {is.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if (socket != null) {try {socket.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if (ss != null) {try {ss.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if (os != null) {try {os.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
}

客户端

package com.ctx.tcp3;import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;/*** 客户端发送照片给服务器* * @author ashen**/
public class TCPClient {public static void main(String[] args)  {new TCPClient().client();}@SuppressWarnings("resource")public void client() {Socket socket = null;OutputStream os = null;FileInputStream fis = null;try {InetAddress inetAddress = InetAddress.getByName("127.0.0.1");socket = new Socket(inetAddress, 4399);os = socket.getOutputStream();File file = new File("D:/images/llj.jpg");fis = new FileInputStream(file);byte[] buffer = new byte[50024];int len;while ((len = fis.read(buffer)) != -1) {os.write(buffer, 0, len);}socket.shutdownOutput();InputStream is = socket.getInputStream();byte[] b = new byte[3024];int length = is.read(b);String msg = new String(b, 0, length);System.out.println(msg);} catch (Exception e) {} finally {if (fis != null) {try {fis.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if (os != null) {try {os.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if (socket != null) {try {socket.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}}

socket传输案例相关推荐

  1. python 网络编程之Socket通信案例消息发送与接收

    背景 网络编程是python编程中的一项基本技术.本文将实现一个简单的Socket通信案例消息发送与接收 正文 在python中的socket编程的大致流程图如上所示 我们来首先编写客户端的代码: # ...

  2. socket传输结构体,c++,发送OK,recv返回字节大小正确但接受数据为空

    socket传输结构体,c++,发送OK,recv返回字节大小正确但接受数据为空 服务端在ubuntu服务器下,客户端在windows下,采用socket进行通信,在客户端接收数据时,出现了诡异的情况 ...

  3. linux c 图像处理,基于uClinux的图像处理及Socket传输的实现-计算机应用与软件.PDF...

    基于uClinux的图像处理及Socket传输的实现-计算机应用与软件 第32卷第4期 计算机应用与软件 Vol32No.4 2015年4月 ComputerApplicationsandSoftw ...

  4. java socket 传输压缩文件_java基于socket传输zip文件功能示例

    本文实例讲述了java基于socket传输zip文件的方法.分享给大家供大家参考,具体如下: 服务器端程序: import java.io.*; import java.net.*; import j ...

  5. Linux下使用socket传输文件的C语言简单实现

    转载:http://blog.csdn.net/ljd_1986413/article/details/7940938 服务器程序和客户端程序应当分别运行在两台计算机上. 在运行服务器端的计算机终端执 ...

  6. 用GZIP来压缩socket传输的序列化的类

    用ObjectOutputStream来序列化类再通过socket传输是方便的网络通信方式.但是一旦数据量较大时,天朝小水管就表示压力很大,压缩数据成了不二的选择.很自然的地想到用GZIPOutput ...

  7. 基于TCP的网络实时聊天室(socket通信案例)

    开门见山 一.数据结构Map 二.保证线程安全 三.群聊核心方法 四.聊天室具体设计 0.用户登录服务器 1.查看当前上线用户 2.群聊 3.私信 4.退出当前聊天状态 5.离线 6.查看帮助 五.聊 ...

  8. Java进阶:基于TCP的网络实时聊天室(socket通信案例)

    目录 开门见山 一.数据结构Map 二.保证线程安全 三.群聊核心方法 四.聊天室具体设计 0.用户登录服务器 1.查看当前上线用户 2.群聊 3.私信 4.退出当前聊天状态 5.离线 6.查看帮助 ...

  9. 浅谈socket传输文件速率优化

    socket传输文件速率优化 前言 最近有个需求,就是在需要提升换机助手的传输文件的传输速度.先来看看什么是换机助手. 一般厂家的换机助手都长这个样子,就是将旧手机的一些数据拷贝到新手机上去.数据一般 ...

最新文章

  1. python deque双端队列的神奇用法
  2. pandas 分组统计的三个函数 pivot table crosstab groupby
  3. 通过运行时单步调试弄清楚[(ngModel)]的双向绑定的工作原理
  4. 【IoT最佳实践】设备获取实时天气DEMO代码解读
  5. 【转载】Linux平台软件包管理完全攻略
  6. FPGA学无止境(目录篇)
  7. android 后退按钮,如何在android中处理Search View的后退按钮
  8. WebLogic(12C)——windows下安装教程
  9. ABBYY FineReader 12 破解版(附注册码)
  10. 搭建国产化统信UOS操作系统虚拟机
  11. JS实现自定义右键菜单
  12. ps2020 快捷键命令简介
  13. 企业网站开发需要注意什么事项?
  14. 公司内部搭建DHCP和DNS服务器
  15. 远程桌面链接怎么共享本地磁盘
  16. 520送什么给男朋友最好?送男朋友礼物排行榜
  17. C#和倍福PLC之间的通信
  18. 自用 Java 学习(JDBC)
  19. IDEA中使用Git功能和IDEA中的Git分支管理
  20. JAVA架构之路(数据加密与常见加密算法)

热门文章

  1. 拆掉思维里的墙-摘抄
  2. Invalid configuration of tez jars, tez.lib.uris is not defined in the configuration
  3. 0、本专栏的预计更新的内容与更新时间表(2022-05-07更新目录排版)
  4. Android M及以上版本系统 悬浮窗权限 的解决方案
  5. m4a批量转换成mp3的方法
  6. win10自带磁盘测速工具
  7. php 增加空行,php 替换空行 不匹配空行
  8. Hibernate框架基础——cascade属性
  9. 上海亚商投顾:沪指低开高走 锂矿股午后大涨
  10. block使用时的一些情况以及防止循环引用