我正在本地测试,所以要连接的IP可以是localhost或127.0.0.1
发送后会收到一个字符串,这也会很方便。
我正在本地测试,所以要连接的IP可以是localhost或127.0.0.1
发送后会收到一个字符串,这也会很方便。
Socket soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
System.Net.IPAddress ipAdd = System.Net.IPAddress.Parse(server);
System.Net.IPEndPoint remoteEP = new IPEndPoint(ipAdd, 3456);
soc.Connect(remoteEP);
连接它的方法。 发送数据:
//Start sending stuf..
byte[] byData = System.Text.Encoding.ASCII.GetBytes("un:" + username + ";pw:" + password);
soc.Send(byData);
而对于读取回来的情况...
byte[] buffer = new byte[1024];
int iRx = soc.Receive(buffer);
char[] chars = new char[iRx];
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(buffer, 0, iRx, chars, 0);
System.String recv = new System.String(chars);
soc.Connect(remoteEP);
连接失败,并显示“无法建立连接,因为目标计算机积极拒绝了连接”? - user2852171Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ipAdd = System.Net.IPAddress.Parse(m_ip);
IPEndPoint remoteEP = new IPEndPoint(ipAdd, m_port);
socket.Connect(remoteEP);
byte[] byData = System.Text.Encoding.ASCII.GetBytes("Message");
socket.Send(byData);
socket.Disconnect(false);
socket.Close();