📄 class1.cs
字号:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class SynchronousSocketClient
{
public static void StartClient()
{
byte[] bytes = new byte[1024];
try
{
IPAddress ip=IPAddress.Parse("127.0.0.1");
IPEndPoint remoteEP = new IPEndPoint(ip,5555);
Socket sender = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp );
try
{
sender.Connect(remoteEP);
Console.WriteLine("Socket connected to {0}",
sender.RemoteEndPoint.ToString());
byte[] msg = Encoding.ASCII.GetBytes("This is a test<EOF>");
int bytesSent = sender.Send(msg);
int bytesRec = sender.Receive(bytes);
Console.WriteLine("Echoed test = {0}",Encoding.ASCII.GetString(bytes,0,bytesRec));
sender.Shutdown(SocketShutdown.Both);
sender.Close();
}
catch (ArgumentNullException ane)
{
Console.WriteLine("ArgumentNullException : {0}",ane.ToString());
}
catch (SocketException se)
{
Console.WriteLine("SocketException : {0}",se.ToString());
}
catch (Exception e)
{
Console.WriteLine("Unexpected exception : {0}", e.ToString());
}
}
catch (Exception e)
{
Console.WriteLine( e.ToString());
}
}
public static int Main(String[] args)
{
StartClient();
return 0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -