Skip to main content

C# client talking to python server with double converting to bytes

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;

namespace Client
{
    class Program
    {
        static Socket ClientSocket;
        static void Main(string[] args)
        {
            String IP = "127.0.0.1";
            int port = 9999;

            IPAddress ip = IPAddress.Parse(IP);  //将IP地址字符串转换成IPAddress实例
            ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//使用指定的地址簇协议、套接字类型和通信协议
            IPEndPoint endPoint = new IPEndPoint(ip, port); // 用指定的ip和端口号初始化IPEndPoint实例
            ClientSocket.Connect(endPoint);  //与远程主机建立连接


            Console.WriteLine("开始发送消息");
            byte[] message = Encoding.ASCII.GetBytes("Connect the Server");  //通信时实际发送的是字节数组,所以要将发送消息转换字节
            double a = 0.0000008900056;
            byte[] wordsToSent = BitConverter.GetBytes(a);

            ClientSocket.Send(wordsToSent);
            Console.WriteLine("发送消息为:" + Encoding.ASCII.GetString(wordsToSent));
            byte[] receive = new byte[1024];
            int length = ClientSocket.Receive(receive);  // length 接收字节数组长度
            Console.WriteLine("接收消息为:" + Encoding.ASCII.GetString(receive));
            ClientSocket.Close();  //关闭连接
            Console.Read();
        }
    }
}

Comments

Popular posts from this blog

span[class~="sr-only"]

  The  span[class~="sr-only"]  selector will select any  span  element whose  class   includes   sr-only . Create that selector, and give it a  border  property set to  0 . span [ class ~= "sr-only" ] {    border:   0 ; }

Use Recursion to Create a Range of Numbers

  function   rangeOfNumbers ( startNum ,  endNum ) {    if ( startNum <= endNum )   {      const   arrNumber  =  rangeOfNumbers ( startNum ,  endNum - 1 );      arrNumber . push ( endNum );      return   arrNumber ;   }    else  {      return  [];   }       }; console . log ( rangeOfNumbers ( 6 , 8 ));//[6,7,8] console . log ( rangeOfNumbers ( 3 , 12 )); //[ 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ]

About the Little Lemon receipt maker exercise

 My homework and exercise of the lesson "About the Little Lemon receipt maker exercise" of the class "programming with Javascript" on coursera. const menu = [     {         Dish : "Italian pasta" ,         price : 9.55     },     {         Dish : "Rice with veggies" ,         price : 8.65     },     {         Dish : "Chicken with potatoes" ,         price : 15.55     },     {         Dish : "Vegetarian Pizza" ,         price : 6.45     } ]; function receiptMaker ( arr , bool ) {     if ( bool == false )     {         console . log ( "Prices without tax:" );         arr . forEach ( element => {             console . log ( `Dish: ${ element . Dish } Price (incl.tax):$ ${ element . price } ` );                     });     }     else     {         console . log ( "Prices with 20% tax:" );         arr . forEach ( element => {             console . log ( `Dish: ${ element . Dish } Price (inc