Home > Net >  C # socket data reception
C # socket data reception

Time:09-28

Client to keep the server to send data to me at this point buff [8]=0 x11, after the server receives, through calculation, send a data to the client, the client receives the service after the end data, return to a piece of data, this data is used to confirm the client has accepted send data to the server, the buff [8]=0 x12, and after the program is running, I can only receive 2 0 x11 data, the rest are 0 x12 data, but I need to store data is 0 x11, how float temp; The Socket serverSocket.//define a socket object Thread listenThread;//define listener Thread Thread threadReceive;//define receive client data thread Socket Socket; DateTime t; Byte p1=0 x00; Byte p2=0 x64; Float voltage_Value; Private void button1_Click (object sender, EventArgs e) {IPAddress IP=IPAddress. Parse (this) text_ip) Text. The Trim ()); ServerSocket=new Socket (AddressFamily. InterNetwork, SocketType Stream, ProtocolType. Tcp);//define a socket try {serverSocket. Bind (new IPEndPoint (IP, Convert ToInt32 (this) text_port) Text. The Trim ()))); ServerSocket. Listen (10);//listening modes listenThread=new Thread (ListenClientConnect); ListenThread. Start ();//start method to run this thread. Button_start. Enabled=false; This. Invoke (new Action (()=& gt; {text_log1. Text +="monitor success" + "\ r \ n"; })); } catch {MessageBox. Show (" abnormal listening ", "listening to the abnormal"); {}} private void ListenClientConnect () while (true) {socket=serverSocket. The Accept ();//listen to the client connection, access to both sides of the communication socket threadReceive=new Thread (Receive);//create a thread loop to receive the client sends data threadReceive. Start (socket); Introduced into both sides communication//}} private void the Receive (object socket) {//receiving client data try {socket myClientSocket=socket (socket); While (true) {byte [] buff=new byte [1024 * 1024 * 2];//receiving data array int r=myClientSocket. The Receive (buff);//receiving data if (buff [0]!=0 x5a & amp; & Buff [1]. {=0 xa5) break; } the if (buff [2].=00 & amp; & Buff [3].={01) break; } the if (buff [4].=00 & amp; & Buff [5].={01) break; }//if (buff [6]==0 x00 & amp; & Buff [7]==0 x08) if (buff [8]==0 x11) {byte check=(byte) (buff + buff [8] [9] + buff + buff [10] [11] + buff + buff [12] [13] + buff [14]). If (buff [15]==check) {t=a DateTime. Now; String T=t.T oString (); Temp=BitConverter. ToSingle (buff, 9); String TEMP=TEMP. ToString (); Strings str1="current temperature for" + temp; This. Invoke (new Action (()=& gt; {text_log1. Text + + str1=T + "\ r \ n"; })); Float f2; Float. TryParse (TEMP, out f2); SQL=new SQL (SQL); SQL. ExecSQL (f2, t); }} the if (buff [8]==0 x12) {Console. WriteLine (" AAA "); } pid V=new pid (); Voltage_Value=https://bbs.csdn.net/topics/V.mPIDCalc (temp, 50); Float Spwm=voltage_Value * 330; If (Spwm) <0 {Spwm=0; } short PWM=the Convert. ToInt16 (Spwm); P1=(byte) (PWM> 8); The p2=(byte) (PWM & 0 x00ff); Console. WriteLine (p1); Console. WriteLine (p2); Byte [] buffer=new byte [18]. Buffer [0]=0 x5a; Buffer [1]=0 xa5; Buffer [2]=0 x00; Buffer [3]=0 x01; Buffer [4]=0 x00; Buffer [5]=0 x01; Buffer [6]=0 x00; Buffer [7]=0 x0a; Buffer [8]=0 x11; String year=DateTime. Now. The year. The ToString (); Byte [] timeyear=new byte [4]. Timeyear=System. Text. Encoding. The Default. GetBytes (year); Buffer [9]=(byte) ((timeyear [2] - 48) * 10 + timeyear [3] - 48). Buffer [10]=(byte) (DateTime. Now, the Month). Buffer [11]=(byte) (DateTime. Now the Day); Buffer [12]=(byte) (DateTime. Now the Hour). Buffer [13]=(byte) (DateTime. Now, Minute); Buffer [14]=(byte) (DateTime. Now. The Second); Buffer [15]=(byte (p1); Buffer [16]=p2 (byte); Byte sendcheck=(byte) (buffer + buffer [8] [9] + buffer + buffer [10] [11] + buffer buffer [12] + [13] + buffer buffer [14] + [15] + buffer [16]); Buffer [17]=sendcheck; MyClientSocket. Send (buffer); Catch}} {MessageBox. Show (" receiving data failure ", "receiving data failure"); }}//receiving data and deposited in the database

CodePudding user response:

Code is too messy, tidy again hair

CodePudding user response:

Old question, glue bag, subcontract, myClientSocket. Do you think is the Receive a data, actually not necessarily

As for "I can only receive 2 0 x11 data", actually does not necessarily

Open the right way, the data stream processing - "determine stick package, subcontract (of course, handle exceptions, but usually internal system can't do anomaly judgement, because debugging stability does not exist this kind of situation, if the external system, we can't guarantee other people submit format must be legitimate)


If you feel for a will can't understand, you can let you have the sender, each hair a 200-500 milliseconds, dormancy can temporarily to a certain extent, solve the problem, but hidden dangers always there just a bit less likely (packet, of course, if it is a big bag, TCP nature of the subcontract, you sleep also make uncertain)
  •  Tags:  
  • C#
  • Related