首页 > 代码库 > Soket 连接发送与接收

Soket 连接发送与接收

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
///url:地址     data:数据
 public static string GetContentFromUrl(string url, string data)
    {
        Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
         
            IPEndPoint ip = new IPEndPoint(IPAddress.Parse(url), 端口);
            socket.Connect(ip);//连接
            char[] cmgs = data.ToCharArray(); //字符串转字符数组
            byte[] message = new byte[1024];
            message = Encoding.UTF8.GetBytes(data);
            socket.Send(message);   //发送数据
        
            byte[] bytes = new byte[1024];
            socket.Receive(bytes);   //接收数据
            string messages = Encoding.Default.GetString(bytes, 0, bytes.Length);
 
            return messages;   //返回数据
        
            socket.Close();
       
 
 
    }