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();
} }
}

最新文章

  1. gfortran编译Fortran数组问题
  2. IIS 7 应用程序池自动回收关闭的解决方案
  3. iOS 直播(一)
  4. 删除SSMS中保存的帐户信息
  5. Spring AOP和AspectJ支持
  6. POJ 1679 The Unique MST (次小生成树)
  7. maven常见命令总结
  8. Unity 安卓下DLL热更新一(核心思想)
  9. 给EasyUI的DateBox控件添加清除button
  10. Flink
  11. [luogu3391][bzoj3223]文艺平衡树【splay】
  12. Sql 按照指定天数时间段查询
  13. windows常用命令积累
  14. LInux下设置账号有效时间 以及 修改用户名(同时修改用户组名和家目录)
  15. CentOS6.9 网络设置
  16. 《Python自动化运维之路》 系统基础信息模块(一)
  17. .NET/ASP.NET/C#/WCF/SQL Server/My SQL/Java/JSP/JDBC/Spring/Spring MVC/PHP/Python/Ruby/Shell/Agile/CSS/HTML/HTTP/Unix/Linux大量PDF书籍/电子书籍下载, Effective Java 下载
  18. MySql避免重复插入记录的几种方法
  19. Android-Java-多线程
  20. core EFCore 开始尝试

热门文章

  1. poj1556 The Doors(叉积判断线段相交)
  2. PTA(Advanced Level)1041.Be Unique
  3. tesseract 3.04在centos6上安装
  4. Mac查看端口号是否被占用及释放
  5. python+vsCode 环境搭建
  6. Python算法题(三)——经典函数题
  7. 搞懂String、StringBuffer、StringBuilder的区别
  8. python计算出现错误
  9. SQL学习——SELECT INTO和INSERT INTO SELECT
  10. Spring @Scheduled执行原理解析