TcpReceive

  1. public Form1()
  2. {
  3. InitializeComponent();
  4. new Thread(() =>
  5. {
  6. IPAddress ip = IPAddress.Parse(ip地址);
  7. Int32 port = ;
  8. TcpListener listen = new TcpListener(ip, port);
  9. listen.Start();
  10. TcpClient tc = listen.AcceptTcpClient();
  11.  
  12. NetworkStream ns = tc.GetStream();
  13. StreamReader sr = new StreamReader(ns);
  14. string result = sr.ReadToEnd();
  15.  
  16. Invoke(new MethodInvoker(delegate() { textBox1.Text = result; }));
  17. sr.Close();
  18. ns.Close();
  19. tc .Close();
  20. listen.Stop();
  21.  
  22. }).Start();
  23. }

TcpSend

  1. public partial class Form1 : Form
  2. {
  3. public Form1()
  4. {
  5. InitializeComponent();
  6. }
  7.  
  8. private void button1_Click(object sender, EventArgs e)
  9. {
  10. TcpClient client = new TcpClient(ip地址, Int32.Parse(端口号));
  11. NetworkStream ns = client.GetStream();
  12. FileStream fs = File.Open("Form1.cs", FileMode.Open);
  13. int data = fs.ReadByte();
  14. while(data!=-)
  15. {
  16. ns.WriteByte((byte)data);
  17. data = fs.ReadByte();
  18. }
  19. fs.Close();
  20. ns.Close();
  21. client.Close();
  22. }
  23. }

Socket Client

  1. static void Main(string[] args)
  2. {
  3.  
  4. IPHostEntry ipHsot = Dns.Resolve(ip地址);
  5. IPAddress ipAddress = ipHsot.AddressList[];
  6. IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, );
  7. Console.WriteLine("Starting : Creating Socket object");
  8. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  9. s.Connect(ipEndPoint);
  10. Console.WriteLine("Successfully Connected to {0}", s.RemoteEndPoint);
  11. while (true)
  12. {
  13. byte[] receivedBytes = new byte[];
  14. string sendMessage = Console.ReadLine();
  15. if (sendMessage.Contains("exit"))
  16. break;
  17. Console.WriteLine("Creating message : {0}", sendMessage);
  18. byte[] forwardMessage = new UTF8Encoding().GetBytes(sendMessage + "[FINAL]");
  19.  
  20. if (s.Connected)
  21. {
  22. try
  23. {
  24. s.Send(forwardMessage);
  25. int totalBytesReceived = s.Receive(receivedBytes);
  26. Console.WriteLine("Message provided from server :{0}", new UTF8Encoding().GetString(receivedBytes, , totalBytesReceived));
  27. }
  28. catch (Exception ex)
  29. {
  30. throw ex;
  31. }
  32. }
  33. }
  34. s.Shutdown(SocketShutdown.Both);
  35. s.Close();
  36.  
  37. }

Socket Server

  1. static void Main(string[] args)
  2. {
  3. Console.WriteLine("Starting : Createing Socket object");
  4. Socket lisner = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  5. lisner.Bind(new IPEndPoint(IPAddress.Any, ));
  6. lisner.Listen();
  7. Console.WriteLine("Waiting for connection on port 13745");
  8. Socket socket = lisner.Accept();
  9. while (true)
  10. {
  11. string receivedValue = string.Empty;
  12. while (true)
  13. {
  14. byte[] receivedBytes = new byte[];
  15. int numBytes = socket.Receive(receivedBytes);
  16. Console.WriteLine("Receiving / ");
  17. receivedValue += new UTF8Encoding().GetString(receivedBytes, , numBytes);
  18. if (receivedValue.IndexOf("[FINAL]") > -)
  19. break;
  20. }
  21. Console.WriteLine("Received value: {0}", receivedValue);
  22.  
  23. string replyValue = Console.ReadLine();
  24. byte[] replyMessage = new UTF8Encoding().GetBytes(replyValue);
  25. socket.Send(replyMessage);
  26. // socket.Shutdown(SocketShutdown.Both);
  27. //socket.Close();
  28. }
  29.  
  30. }
  31. }

C# Tcp和Socket 网络(五)的更多相关文章

  1. Java Web 基础(一) 基于TCP的Socket网络编程

    一.Socket简单介绍 Socket通信作为Java网络通讯的基础内容,集中了异常.I/O流模式等众多知识点.学习Socket通信,既能够了解真正的网络通讯原理,也能够增强对I/O流模式的理解. 1 ...

  2. Socket网络编程详解

    一,socket的起源 socket一词的起源 在组网领域的首次使用是在1970年2月12日发布的文献IETF RFC33中发现的, 撰写者为Stephen Carr.Steve Crocker和Vi ...

  3. Socket网络编程基本介绍

    一,socket的起源 socket一词的起源 在组网领域的首次使用是在1970年2月12日发布的文献IETF RFC33中发现的, 撰写者为Stephen Carr.Steve Crocker和Vi ...

  4. 网络编程懒人入门(八):手把手教你写基于TCP的Socket长连接

    本文原作者:“水晶虾饺”,原文由“玉刚说”写作平台提供写作赞助,原文版权归“玉刚说”微信公众号所有,即时通讯网收录时有改动. 1.引言 好多小白初次接触即时通讯(比如:IM或者消息推送应用)时,总是不 ...

  5. Python进阶(1)_Socket网络编程(基于tcp的socket)

    网络协议参考:http://www.cnblogs.com/hedeyong/p/6889774.html 一.TCP/IP五层模型 学习socket一定要先学习互联网协议: 1.首先:本节课程的目标 ...

  6. Socket网络编程TCP、UDP演示样例

    Socket网络编程: 1) OSI(了解): 国际标准化组织ISO(International Orgnization for Standardization)指定了网络通信的模型:开放系统互联(O ...

  7. Socket网络编程(TCP/IP/端口/类)和实例

    Socket网络编程(TCP/IP/端口/类)和实例 原文:C# Socket网络编程精华篇 转自:微冷的雨 我们在讲解Socket编程前,先看几个和Socket编程紧密相关的概念: TCP/IP层次 ...

  8. python 网络编程 TCP/IP socket UDP

    TCP/IP简介 虽然大家现在对互联网很熟悉,但是计算机网络的出现比互联网要早很多. 计算机为了联网,就必须规定通信协议,早期的计算机网络,都是由各厂商自己规定一套协议,IBM.Apple和Micro ...

  9. Day09: socket网络编程-OSI七层协议,tcp/udp套接字,tcp粘包问题,socketserver

    今日内容:socket网络编程    1.OSI七层协议    2.基于tcp协议的套接字通信    3.模拟ssh远程执行命令    4.tcp的粘包问题及解决方案    5.基于udp协议的套接字 ...

随机推荐

  1. #【Python】【demo实验23】【练习实例】【 三人比赛顺序问题 】

    原题: 两个乒乓球队进行比赛,各出三人.甲队为a,b,c三人,乙队为x,y,z三人.已抽签决定比赛名单.有人向队员打听比赛的名单.a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单. 我的 ...

  2. 使用window.open 实现弹框和居中对齐

    // 打开页面方法 window.open(url, '_blank', centerStyle('600', '400')+',toolbar=no,menubar=no,resizeable=no ...

  3. Double write Buffer的配置

    InnoDB和XtraDB使用称为doublewrite缓冲区的特殊功能来提供数据损坏的强大保证.想法是在写入数据文件之前将数据写入主表空间中的顺序日志.如果发生部分页面写入(换句话说,写入损坏),I ...

  4. windows10下无U盘安装ubuntu18 使用EasyUEFI(一点点体会)

    一.看BIOS 先看看自己电脑的是哪种启动模式  win+R 输入 msinfo32  查看自己电脑是哪种 (UEFI还是Legacy BIOS启动模式) 查看完之后  如果是UEFI的话 go on ...

  5. StorageClass-动态PVC

    StorageClass 之前我们部署了PV 和 PVC 的使用方法,但是前面的 PV 都是静态的,什么意思?就是我要使用的一个 PVC 的话就必须手动去创建一个 PV,我们也说过这种方式在很大程度上 ...

  6. 测试工作小工具~总结&下载连接

    1.Gif录制小工具(动图提单 ≖ᴗ≖) 地址:https://licecap.en.softonic.com/download

  7. CSS实现单选按钮

    import React from 'react' import PropTypes from 'prop-types' import CX from 'classnames' import _ fr ...

  8. 论文阅读:Adaptive NMS: Refining Pedestrian Detection in a Crowd

    论文阅读:Adaptive NMS: Refining Pedestrian Detection in a Crowd 2019年04月11日 23:08:02 Kivee123 阅读数 836   ...

  9. 数据库分库分表策略之MS-SQL读写分离方案

    MS-SQL读写分离将从以下知识点进行展开: 以下截图内容来自博主:https://www.cnblogs.com/echosong/p/3603270.html 1.本地发布(写库如:centerd ...

  10. ggpubr进行“paper”组图合并,也许比PS,AI更简单

    本文转载自微信公众号 “生信补给站”,https://mp.weixin.qq.com/s/41iKTulTwGcY-dHtqqSnLA 多个图形进行组图展示,可以既展示一个“事情”的多个角度,也可以 ...