C# Tcp和Socket 网络(五)
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 网络(五)的更多相关文章
- Java Web 基础(一) 基于TCP的Socket网络编程
一.Socket简单介绍 Socket通信作为Java网络通讯的基础内容,集中了异常.I/O流模式等众多知识点.学习Socket通信,既能够了解真正的网络通讯原理,也能够增强对I/O流模式的理解. 1 ...
- Socket网络编程详解
一,socket的起源 socket一词的起源 在组网领域的首次使用是在1970年2月12日发布的文献IETF RFC33中发现的, 撰写者为Stephen Carr.Steve Crocker和Vi ...
- Socket网络编程基本介绍
一,socket的起源 socket一词的起源 在组网领域的首次使用是在1970年2月12日发布的文献IETF RFC33中发现的, 撰写者为Stephen Carr.Steve Crocker和Vi ...
- 网络编程懒人入门(八):手把手教你写基于TCP的Socket长连接
本文原作者:“水晶虾饺”,原文由“玉刚说”写作平台提供写作赞助,原文版权归“玉刚说”微信公众号所有,即时通讯网收录时有改动. 1.引言 好多小白初次接触即时通讯(比如:IM或者消息推送应用)时,总是不 ...
- Python进阶(1)_Socket网络编程(基于tcp的socket)
网络协议参考:http://www.cnblogs.com/hedeyong/p/6889774.html 一.TCP/IP五层模型 学习socket一定要先学习互联网协议: 1.首先:本节课程的目标 ...
- Socket网络编程TCP、UDP演示样例
Socket网络编程: 1) OSI(了解): 国际标准化组织ISO(International Orgnization for Standardization)指定了网络通信的模型:开放系统互联(O ...
- Socket网络编程(TCP/IP/端口/类)和实例
Socket网络编程(TCP/IP/端口/类)和实例 原文:C# Socket网络编程精华篇 转自:微冷的雨 我们在讲解Socket编程前,先看几个和Socket编程紧密相关的概念: TCP/IP层次 ...
- python 网络编程 TCP/IP socket UDP
TCP/IP简介 虽然大家现在对互联网很熟悉,但是计算机网络的出现比互联网要早很多. 计算机为了联网,就必须规定通信协议,早期的计算机网络,都是由各厂商自己规定一套协议,IBM.Apple和Micro ...
- Day09: socket网络编程-OSI七层协议,tcp/udp套接字,tcp粘包问题,socketserver
今日内容:socket网络编程 1.OSI七层协议 2.基于tcp协议的套接字通信 3.模拟ssh远程执行命令 4.tcp的粘包问题及解决方案 5.基于udp协议的套接字 ...
随机推荐
- 小程序部分机型上一个诡异的偶现bug
如上图所示:开始的时候进到下单页面,价格是0,当选中了商品产生价格的时候,生成的价格如 ¥150,这个时候会只露出¥1以及一小半的5,后面的都被遮挡住了. wxml里是这样的写的 <view w ...
- 《MIT 6.828 Homework 1: boot xv6》解题报告
本作业的网站链接:MIT 6.828 Homework 1: boot xv6 问题 Exercise: What is on the stack? While stopped at the abov ...
- [CF261E]Maxim and Calculator_搜索_欧拉筛素数_动态规划
Maxim and Calculator 题目链接:https://www.luogu.org/problem/CF261E 数据范围:略. 题解: 考试的时候只会暴力,学弟太强了$\%\%\% Or ...
- js中构造函数与普通函数的区别
构造函数不仅只出现在JavaScript中,它同样存在于很多主流的程序语言里,比如c++.Java.PHP等等.与这些主流程序语言一样,构造函数在js中的作业一样,也是用来创建对象时初始化对象,并且总 ...
- Mysql解析json字符串/数组
1 Mysql解析json字符串 解决方法:JSON_EXTRACT(原字段,'$.json字段名') 执行SQL: SELECT JSON_EXTRACT( t.result,'$.row'), ...
- Netty源码剖析-构建链接
参考文献:极客时间傅健老师的<Netty源码剖析与实战>Talk is cheap.show me the code! ----主线: 和启动一样也是有两个线程完成的,boss threa ...
- [游戏复刻] 2048(2014. Android)
等哪一天我有很多很多的时间再写吧...
- Java手写简单HashMap一(包括增加,查找,toString,泛型)
@Java 300 学习总结 HashMap底层采用实现采用了哈希表,结合了"数组和链表". 原理如图 一.定义HashMap类. 首先需要定义一个节点来存储数据,构成链表结构. ...
- 数据结构和算法总结(三):A* 寻路算法
前言 复习下寻路相关的东西,而且A star寻路在游戏开发中应用挺多的,故记录下. 正文 迪杰斯特拉算法 说起A*得先谈谈Dijkstra算法,它是在BFS基础上的一种带权值的两点最短寻路贪心算法. ...
- R-corrplot相关性绘图,只有你想不到的
初步接触数据集,探索性分析后,经常需要做一个相关分析,得到各变量间的相关系数以及显著性水平. 本文介绍一下R-corrplot包进行相关可视化展示. 一 数据准备 载入所需的R包,利用公共数据集mtc ...