参考: private void sendSocket( string sendData )
{
using ( Socket socket = new Socket( AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp ) )
{
IPHostEntry iPHostEntry = Dns.GetHostByName( ServerIp ); 
IPAddress[] iPAddressArr = iPHostEntry.AddressList; 
IPAddress iPAddress = iPAddressArr[0]; 
IPEndPoint iPEndPoint = new IPEndPoint( iPAddress, Port );
try
{
socket.Connect( iPEndPoint );
}
catch
{
ResponseContent = "连接异常!";
throw;
}
byte[] sendDataBytes = new byte[ sendData.Length ]; 
sendDataBytes = Encoding.GetEncoding("GB2312").GetBytes( sendData );
socket.Send( sendDataBytes, sendDataBytes.Length, 0 ); 
byte[] recvBytes = new byte[1024]; 
int recvBytesLength = socket.Receive( recvBytes, recvBytes.Length, 0 ); 
ResponseContent = Encoding.GetEncoding("GB2312").GetString( recvBytes, 0, recvBytesLength ); 
socket.Shutdown( SocketShutdown.Both );  
}
}

解决方案 »

  1.   

    c的语法和c#的差不多  先看看c#的语法 在试着转换 应该没问题的
      

  2.   

    恩,我知道,我已经转换过了,但是由于总是有问题,所以我想看一下别人的转换,对比自己写的是否正确下面是我转换的代码
    Socket newsock = new Socket(AddressFamily.InterNetwork,
    SocketType.Dgram,0);
    IPAddress ip=IPAddress.Parse("172.31.1.21");

    IPEndPoint sender = new IPEndPoint(ip,8089); EndPoint Remote = (EndPoint)(sender); newsock.SendTo(netdata,netdata.Length,0,Remote);
    newsock.Close();