network-scoket
server:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets; namespace net_server
{
class Program
{
static void Main(string[] args)
{
int get = ;
const int size = ;
Console.WriteLine("Server is Running...");
IPAddress ip = new IPAddress(new byte []{,,,});
//IPAddress ip = Dns.GetHostEntry("localhost").AddressList[0]; //另一种获取ip的方法
TcpListener listener = new TcpListener(ip, );
listener.Start();
Console.WriteLine("start listenering ..."); TcpClient remoteclient = listener.AcceptTcpClient();//获取侦听,返回一个TcpClient
Console.WriteLine("client connected!{0}<---{1}", remoteclient.Client.LocalEndPoint, remoteclient.Client.RemoteEndPoint); NetworkStream streamtoclient = remoteclient.GetStream();//得到客户端的数据流
do
{
byte[] buffer = new byte[size];
try
{
lock (streamtoclient)
{
get = streamtoclient.Read(buffer, , size);//获得长度,这个方法是同步的,只有客户端发送数据才会执行,否则便会一直等待
}
Console.WriteLine("get ,{0} byte", get); //获得请求字符创
string msg = Encoding.Unicode.GetString(buffer, , size);
Console.WriteLine("read:{0}", msg); msg = msg.ToUpper();
buffer = Encoding.Unicode.GetBytes(msg);
lock (streamtoclient)
{
streamtoclient.Write(buffer, , buffer.Length);
}
Console.WriteLine("send:{0}", msg);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
break;
}
} while (true);
streamtoclient.Dispose();
remoteclient.Close(); Console.WriteLine("\n\n输入\"Q\"键退出。");
ConsoleKey key;
do
{
key = Console.ReadKey(true).Key;
} while (key != ConsoleKey.Q);
}
}
}
client:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
namespace net_client
{
class Program
{
static void Main(string[] args)
{
const int size = ;
Console.WriteLine("Client is running...");
TcpClient client ;
try
{
client = new TcpClient();
client.Connect("127.0.0.1", );
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return;
}
Console.WriteLine("Server connected!{0}--->{1}", client.Client.LocalEndPoint, client.Client.RemoteEndPoint); NetworkStream streamtoserver = client.GetStream();
ConsoleKey keys;
do
{
keys = Console.ReadKey (true).Key;
if (keys == ConsoleKey.A)//当按下A键可以开始输入发送内容
{
string msg = Console.ReadLine();
byte[] buffer = Encoding.Unicode.GetBytes(msg);
try
{
lock (streamtoserver)
{
streamtoserver.Write(buffer, , buffer.Length);
}
Console.WriteLine("send :{0}", msg); int byteread = ;
buffer = new byte[size];
lock(streamtoserver )
{
byteread = streamtoserver .Read (buffer ,,size);
} msg = Encoding .Unicode.GetString (buffer ,,byteread );
Console .WriteLine ("server con:{0}",msg );
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}while(keys!=ConsoleKey.E);
streamtoserver.Dispose();
client.Close(); //Console.WriteLine("\n\n输入\"Q\"键退出。");
//ConsoleKey key;
//do
//{
// key = Console.ReadKey(true).Key;
//} while (key != ConsoleKey.Q); }
}
}
network-scoket的更多相关文章
- Recurrent Neural Network系列1--RNN(循环神经网络)概述
作者:zhbzz2007 出处:http://www.cnblogs.com/zhbzz2007 欢迎转载,也请保留这段声明.谢谢! 本文翻译自 RECURRENT NEURAL NETWORKS T ...
- 创建 OVS flat network - 每天5分钟玩转 OpenStack(134)
上一节完成了 flat 的配置工作,今天创建 OVS flat network.Admin -> Networks,点击 "Create Network" 按钮. 显示创建页 ...
- 在 ML2 中配置 OVS flat network - 每天5分钟玩转 OpenStack(133)
前面讨论了 OVS local network,今天开始学习 flat network. flat network 是不带 tag 的网络,宿主机的物理网卡通过网桥与 flat network 连接, ...
- OVS local network 连通性分析 - 每天5分钟玩转 OpenStack(132)
前面已经创建了两个 OVS local network,今天详细分析它们之间的连通性. launch 新的 instance "cirros-vm3",网络选择 second_lo ...
- 再部署一个 instance 和 Local Network - 每天5分钟玩转 OpenStack(131)
上一节部署了 cirros-vm1 到 first_local_net,今天我们将再部署 cirros-vm2 到同一网络,并创建 second_local_net. 连接第二个 instance 到 ...
- 创建 OVS Local Network - 每天5分钟玩转 OpenStack(129)
上一节我们完成了 OVS 的准备工作,本节从最基础的 local network 开始学习.local network 不会与宿主机的任何物理网卡连接,流量只被限制在宿主机内,同时也不关联任何的 VL ...
- Configure a bridged network interface for KVM using RHEL 5.4 or later?
environment Red Hat Enterprise Linux 5.4 or later Red Hat Enterprise Linux 6.0 or later KVM virtual ...
- BZOJ 1146: [CTSC2008]网络管理Network [树上带修改主席树]
1146: [CTSC2008]网络管理Network Time Limit: 50 Sec Memory Limit: 162 MBSubmit: 3522 Solved: 1041[Submi ...
- [Network Analysis] 复杂网络分析总结
在我们的现实生活中,许多复杂系统都可以建模成一种复杂网络进行分析,比如常见的电力网络.航空网络.交通网络.计算机网络以及社交网络等等.复杂网络不仅是一种数据的表现形式,它同样也是一种科学研究的手段.复 ...
- POJ 1966 Cable TV Network
Cable TV Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 4702 Accepted: 2173 ...
随机推荐
- P1315 观光公交
题目描述 风景迷人的小城Y 市,拥有n 个美丽的景点.由于慕名而来的游客越来越多,Y 市特意安排了一辆观光公交车,为游客提供更便捷的交通服务.观光公交车在第 0 分钟出现在 1号景点,随后依次前往 2 ...
- BZOJ2120:数颜色——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=2120 https://www.luogu.org/problemnew/show/P1903#su ...
- Kindle 电子书相关的工具软件【转】
这里是与 Kindle 电子书相关的工具软件.它们可以帮助我们解决在日常使用电子书时所可能遇到的问题,比如 kindle 管理工具.kindle 转换工具.kindle电子书制作工具.kindle 推 ...
- odex文件格式
apk安装或启动时,会通过dexopt来将dex生成优化后的odex文件.过程是将apk中的classes.dex解压后,用dexopt处理并保存为“/data/dalvik-cache/data@a ...
- mysql主从同步加读写分离
首先主从同步,一旦建立,指定了用户,就不能更改了,否则会有错误.1063 Error 'Duplicate entry '%-test-' for key 'PRIMARY'' on query. D ...
- Tree and Permutation dfs hdu 6446
Problem Description There are N vertices connected by N−1 edges, each edge has its own length.The se ...
- [python]字符串的ljust方法
ljust用法: string.ljust(number,'x') 格式化输出字符串,按照number数量调整字符串的总长度,ljust是左对齐,‘x’是填充字符,默认是空格 类似的还有rjust,c ...
- [解决] Error Code: 1044. Access denied for user 'root'@'%' to database
今天在测试集群用的mysql上,遇到个权限的问题: SQLException : SQL state: 42000 com.mysql.jdbc.exceptions.jdbc4.MySQLSynta ...
- is(':checked'); 判断radio是否选中,该属性会随着radio被切换点击而变化
// var is_rec =$("#is_rec_on").is(':checked'); //判断radio是否选中,该属性会随着radio被切换点击而变化 // if(is_ ...
- Nginx与Apache工作方式
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://going.blog.51cto.com/7876557/1304204 Ngin ...