TcpReceive

 public Form1()
{
InitializeComponent();
new Thread(() =>
{
IPAddress ip = IPAddress.Parse(ip地址);
Int32 port = ;
TcpListener listen = new TcpListener(ip, port);
listen.Start();
TcpClient tc = listen.AcceptTcpClient(); NetworkStream ns = tc.GetStream();
StreamReader sr = new StreamReader(ns);
string result = sr.ReadToEnd(); Invoke(new MethodInvoker(delegate() { textBox1.Text = result; }));
sr.Close();
ns.Close();
tc .Close();
listen.Stop(); }).Start();
}

TcpSend

  public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
TcpClient client = new TcpClient(ip地址, Int32.Parse(端口号));
NetworkStream ns = client.GetStream();
FileStream fs = File.Open("Form1.cs", FileMode.Open);
int data = fs.ReadByte();
while(data!=-)
{
ns.WriteByte((byte)data);
data = fs.ReadByte();
}
fs.Close();
ns.Close();
client.Close();
}
}

Socket Client

 static void Main(string[] args)
{ IPHostEntry ipHsot = Dns.Resolve(ip地址);
IPAddress ipAddress = ipHsot.AddressList[];
IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, );
Console.WriteLine("Starting : Creating Socket object");
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Connect(ipEndPoint);
Console.WriteLine("Successfully Connected to {0}", s.RemoteEndPoint);
while (true)
{
byte[] receivedBytes = new byte[];
string sendMessage = Console.ReadLine();
if (sendMessage.Contains("exit"))
break;
Console.WriteLine("Creating message : {0}", sendMessage);
byte[] forwardMessage = new UTF8Encoding().GetBytes(sendMessage + "[FINAL]"); if (s.Connected)
{
try
{
s.Send(forwardMessage);
int totalBytesReceived = s.Receive(receivedBytes);
Console.WriteLine("Message provided from server :{0}", new UTF8Encoding().GetString(receivedBytes, , totalBytesReceived));
}
catch (Exception ex)
{
throw ex;
}
}
}
s.Shutdown(SocketShutdown.Both);
s.Close(); }

Socket Server

  static void Main(string[] args)
{
Console.WriteLine("Starting : Createing Socket object");
Socket lisner = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
lisner.Bind(new IPEndPoint(IPAddress.Any, ));
lisner.Listen();
Console.WriteLine("Waiting for connection on port 13745");
Socket socket = lisner.Accept();
while (true)
{
string receivedValue = string.Empty;
while (true)
{
byte[] receivedBytes = new byte[];
int numBytes = socket.Receive(receivedBytes);
Console.WriteLine("Receiving / ");
receivedValue += new UTF8Encoding().GetString(receivedBytes, , numBytes);
if (receivedValue.IndexOf("[FINAL]") > -)
break;
}
Console.WriteLine("Received value: {0}", receivedValue); string replyValue = Console.ReadLine();
byte[] replyMessage = new UTF8Encoding().GetBytes(replyValue);
socket.Send(replyMessage);
// socket.Shutdown(SocketShutdown.Both);
//socket.Close();
} }
}

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. vue-router 在微信浏览器中操作history URl未改变的解决方案

    在PC端和手机浏览器中router.replace() or router.push()能够正常使用,页面的地址和页面都正常显示:但是在微信中,从/a页面通过router.push('/b')跳转到/ ...

  2. orcale备份语句

    1.创建一个文件夹,比如d盘下创建一个expdp的文件夹 d:\expdp2.使用一个用户,必须具有DBA权限 比如 sqlplus /nolog conn system/password@数据库连接 ...

  3. SpringBoot传参方式

    地址传参 1.创建一个Action类 package com.lion.action; import org.springframework.stereotype.Controller; import ...

  4. SQLite进阶-13.Autoincrement关键字

    目录 AUTOINCREMENT 是一个关键字,用于表中的字段值自动递增.我们可以在创建表时在特定的列名称上使用 AUTOINCREMENT 关键字实现该字段值的自动增加. 关键字 AUTOINCRE ...

  5. javaweb项目的全局监听配置

    在项目中有时候会遇到全局监听的需求,而全局性的监听该如何配置,代码如下: package com.demo.listener; import javax.servlet.ServletContextE ...

  6. win10使用vnc远程到Ubuntu 19.04

    主要参考:https://www.cyberciti.biz/faq/install-and-configure-tigervnc-server-on-ubuntu-18-04/ https://ww ...

  7. 【数据结构】P1449 后缀表达式

    [题目链接] https://www.luogu.org/problem/P1449 [题目描述] 所谓后缀表达式是指这样的一个表达式:式中不再引用括号,运算符号放在两个运算对象之后,所有计算按运算符 ...

  8. css加载没效果,查看网络显示类型为 text/plain 的解决方法

    当请求的css 文件的MIME 类型 为 text/plain 时,文件会当做 .txt 文件来处理,浏览器就会拒绝渲染 产生这样的问题和服务器的配置有关系 在服务器上加上下面两句就好了 搞定

  9. php实现拼图滑块验证的思考及部分实现

    实现拼图滑块验证,我觉得其中比较关键的一点就是裁剪图片,最起码需要裁剪出下面两张图的样子 底图 滑块图 一张底图和一张滑块图,其中底图实现起来比较简单可以使用添加水印的方式直接将一张拼图形状的半透明图 ...

  10. ELECTRON 打包

    安装electron-packager cnpm install electron-packager -g 配置package.json "scripts": { "st ...