C# Tcp和Socket 网络(五)
2024-09-05 06:52:38
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();
} }
}
最新文章
- gfortran编译Fortran数组问题
- IIS 7 应用程序池自动回收关闭的解决方案
- iOS 直播(一)
- 删除SSMS中保存的帐户信息
- Spring AOP和AspectJ支持
- POJ 1679 The Unique MST (次小生成树)
- maven常见命令总结
- Unity 安卓下DLL热更新一(核心思想)
- 给EasyUI的DateBox控件添加清除button
- Flink
- [luogu3391][bzoj3223]文艺平衡树【splay】
- Sql 按照指定天数时间段查询
- windows常用命令积累
- LInux下设置账号有效时间 以及 修改用户名(同时修改用户组名和家目录)
- CentOS6.9 网络设置
- 《Python自动化运维之路》 系统基础信息模块(一)
- .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 下载
- MySql避免重复插入记录的几种方法
- Android-Java-多线程
- core EFCore 开始尝试