📄 class1.cs
字号:
using System;
using System.Net;
using System.Net.Sockets;
class Client
{
static void Main(string[] args)
{
IPAddress remoteHost=IPAddress.Parse("127.0.0.1");
IPEndPoint iep=new IPEndPoint(remoteHost,5555);
Socket mySocket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
try
{
mySocket.Connect(iep);
byte []buf=new byte[1024];
int k=mySocket.Receive(buf);
System.Console.WriteLine(System.Text.Encoding.Unicode.GetString(buf,0,k));
mySocket.Send(System.Text.Encoding.Unicode.GetBytes("服务器你好"));
k=mySocket.Receive(buf);
System.Console.WriteLine(System.Text.Encoding.Unicode.GetString(buf,0,k));
}
catch(ArgumentNullException ae)
{
Console.WriteLine("ArgumentNullException : {0}", ae.ToString());
}
catch(SocketException se)
{
Console.WriteLine("SocketException : {0}", se.ToString());
}
catch(Exception e)
{
Console.WriteLine("Unexpected exception : {0}", e.ToString());
}
finally
{
mySocket.Shutdown(SocketShutdown.Both);
mySocket.Close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -