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的更多相关文章

  1. Recurrent Neural Network系列1--RNN(循环神经网络)概述

    作者:zhbzz2007 出处:http://www.cnblogs.com/zhbzz2007 欢迎转载,也请保留这段声明.谢谢! 本文翻译自 RECURRENT NEURAL NETWORKS T ...

  2. 创建 OVS flat network - 每天5分钟玩转 OpenStack(134)

    上一节完成了 flat 的配置工作,今天创建 OVS flat network.Admin -> Networks,点击 "Create Network" 按钮. 显示创建页 ...

  3. 在 ML2 中配置 OVS flat network - 每天5分钟玩转 OpenStack(133)

    前面讨论了 OVS local network,今天开始学习 flat network. flat network 是不带 tag 的网络,宿主机的物理网卡通过网桥与 flat network 连接, ...

  4. OVS local network 连通性分析 - 每天5分钟玩转 OpenStack(132)

    前面已经创建了两个 OVS local network,今天详细分析它们之间的连通性. launch 新的 instance "cirros-vm3",网络选择 second_lo ...

  5. 再部署一个 instance 和 Local Network - 每天5分钟玩转 OpenStack(131)

    上一节部署了 cirros-vm1 到 first_local_net,今天我们将再部署 cirros-vm2 到同一网络,并创建 second_local_net. 连接第二个 instance 到 ...

  6. 创建 OVS Local Network - 每天5分钟玩转 OpenStack(129)

    上一节我们完成了 OVS 的准备工作,本节从最基础的 local network 开始学习.local network 不会与宿主机的任何物理网卡连接,流量只被限制在宿主机内,同时也不关联任何的 VL ...

  7. 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 ...

  8. BZOJ 1146: [CTSC2008]网络管理Network [树上带修改主席树]

    1146: [CTSC2008]网络管理Network Time Limit: 50 Sec  Memory Limit: 162 MBSubmit: 3522  Solved: 1041[Submi ...

  9. [Network Analysis] 复杂网络分析总结

    在我们的现实生活中,许多复杂系统都可以建模成一种复杂网络进行分析,比如常见的电力网络.航空网络.交通网络.计算机网络以及社交网络等等.复杂网络不仅是一种数据的表现形式,它同样也是一种科学研究的手段.复 ...

  10. POJ 1966 Cable TV Network

    Cable TV Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4702   Accepted: 2173 ...

随机推荐

  1. PowerDesigner 建表语句没出现字段描述

    填写了Name 还是没有 修改步骤如下:

  2. BZOJ1856:[SCOI2010]字符串——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=1856 lxhgww最近接到了一个生成字符串的任务,任务需要他把n个1和m个0组成字符串,但是任务还 ...

  3. HDU3157:Crazy Circuits——题解

    http://acm.hdu.edu.cn/showproblem.php?pid=3157 题目大意:给一个电路 ,起点为+,终点为-,包括起点终点在内的电元件之间有有下界边,求最小流. ————— ...

  4. HDOJ.1009 FatMouse' Trade (贪心)

    FatMouse' Trade 点我挑战题目 题意分析 每组数据,给出有的猫粮m与房间数n,接着有n行,分别是这个房间存放的食物和所需要的猫粮.求这组数据能保证的最大的食物是多少? (可以不完全保证这 ...

  5. Jsp电子商务之七 订单篇2

    从View页面,点击超链接查询订单,进入到控制器 OrderlistServlet package com.cart.web; import java.io.IOException; import j ...

  6. JavaScript URL汉字编码转换

    在使用url进行参数传递时,经常会传递一些中文名的参数或URL地址,在后台处理时会发生转换错误.在有些传递页面使用GB2312,而在接收页面使用UTF8,这样接收到的参数就可能会与原来发生不一致.使用 ...

  7. ImageNet: what is top-1 and top-5 error rate?

    https://stats.stackexchange.com/questions/156471/imagenet-what-is-top-1-and-top-5-error-rate Your cl ...

  8. [解决] Error Code: 1044. Access denied for user 'root'@'%' to database

    今天在测试集群用的mysql上,遇到个权限的问题: SQLException : SQL state: 42000 com.mysql.jdbc.exceptions.jdbc4.MySQLSynta ...

  9. Mybatis中传入List条件

    传入一个map的参数,map里有一个tenantIds的List,在xml里先判断这个List的size是否大于o,然后通过foreach 构造一个in后面括号里的元素,具体的xml如下: <i ...

  10. WPF 与设备无关的单位

    WPF从发布之日起,一直将“分辨率无关(resolution independence)”作为其亮点,声称使用WPF制作的用户界面在轻巧的Ultra-Mobile PC的屏幕上和在50英寸的电视机上都 ...