| |

VerySource

 Forgot password?
 Register
Search
View: 961|Reply: 9

Questions about TCP communication

[Copy link]

1

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-1-24 22:00:01
| Show all posts |Read mode
When TCP monitoring, there is a client connection, how to receive the client's information and send the message to the client immediately?
TcpClient MyTcpClient = MyTcpListener.AcceptTcpClient ();
NetworkStream MyStream = MyTcpClient.GetStream ();
byte [] MyBytes = new byte [1024];
int MyBytesRead = MyStream.Read (MyBytes, 0, MyBytes.Length);
string MyMessage = System.Text.Encoding.Default.GetString (MyBytes, 0, MyBytesRead);
cmd = MyMessage;
this.richTextBox1.Text + = cmd;
Ranch
cmd = "HELLO WORLD!";
StreamWriter MyStreamS = new StreamWriter (MyStream);
MyStreamS.Write (cmd);
MyStreamS.Flush ();
MyStreamS.Close ();
MyTcpClient.Close ();
Reply

Use magic Report

0

Threads

14

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-2-11 22:15:01
| Show all posts
The MyStream.Write (...) obtained directly with GetStream ();
Reply

Use magic Report

1

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-2-12 10:45:01
| Show all posts
But this also prompts errors
TcpClient MyTcpClient = MyTcpListener.AcceptTcpClient ();
NetworkStream MyStream = MyTcpClient.GetStream ();
byte [] MyBytes = new byte [1024];
int MyBytesRead = MyStream.Read (MyBytes, 0, MyBytes.Length);
string MyMessage = System.Text.Encoding.Default.GetString (MyBytes, 0, MyBytesRead);
cmd = MyMessage;
this.richTextBox1.Text + = cmd.Substring (4,11);
// Explain execution
cmd = "HELLO WORLD!";
MyStream.Write (cmd);
MyStream.Flush ();
MyStream.Close ();
MyTcpClient.Close ();
Reply

Use magic Report

1

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-2-12 17:45:02
| Show all posts
The following method has not been resolved.
TcpClient MyTcpClient = MyTcpListener.AcceptTcpClient ();
NetworkStream MyStream = MyTcpClient.GetStream ();
byte [] MyBytes = new byte [1024];
int MyBytesRead = MyStream.Read (MyBytes, 0, MyBytes.Length);
string MyMessage = System.Text.Encoding.Default.GetString (MyBytes, 0, MyBytesRead);
cmd = MyMessage;
this.richTextBox1.Text + = cmd.Substring (4,11);
cmd = "HELLO WORLD!";
byte [] byteTime = Encoding.ASCII.GetBytes (cmd);
MyStream.Write (byteTime, 0, byteTime.Length);
//MyStream.Flush ();
MyStream.Close ();
MyTcpClient.Close ();
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-3-23 23:00:01
| Show all posts
TcpListener MyTcpListener = new TcpListener (9999);
            MyTcpListener.Start ();
            TcpClient MyTcpClient = MyTcpListener.AcceptTcpClient ();
            NetworkStream MyStream = MyTcpClient.GetStream ();
            byte [] MyBytes = new byte [1024];
            int MyBytesRead = MyStream.Read (MyBytes, 0, MyBytes.Length);
            string MyMessage = System.Text.Encoding.Default.GetString (MyBytes, 0, MyBytesRead);
            string cmd = MyMessage;
            //this.richTextBox1.Text + = cmd.Substring (4, 11);
            cmd = "HELLO WORLD!";
            byte [] byteTime = Encoding.ASCII.GetBytes (cmd);
            MyStream.Write (byteTime, 0, byteTime.Length);
            //MyStream.Flush ();
            MyStream.Close ();
            MyTcpClient.Close ();
            Console.Read ();
Reply

Use magic Report

1

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-4-14 02:45:01
| Show all posts
This big brother upstairs seems to make no difference
Reply

Use magic Report

1

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-4-14 17:30:02
| Show all posts
I don't understand the meaning of the upstairs, can you give me some pointers?
Reply

Use magic Report

0

Threads

119

Posts

67.00

Credits

Newbie

Rank: 1

Credits
67.00

 China

Post time: 2020-4-15 16:00:02
| Show all posts
Asynchronous callback method:
mainSocket.BeginAccept (new AsyncCallback (OnClientConnect), null);
The landlord can refer to relevant information!
Reply

Use magic Report

1

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-4-17 12:30:01
| Show all posts
My idea is that after receiving the message from the client on the server, it is displayed on the server, and then immediately send a message from the server to the client. This should not be responsible, but why I clicked the code before me but the client cannot receive the service Information. Can you give me a piece of information, thank you
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 Singapore

Post time: 2020-8-25 20:00:01
| Show all posts
My idea is to display the message on the server after receiving the message from the client
= There is no problem at all, just asynchronous reception

Then immediately send a message from the server to the client
= Here comes the problem: sending messages is fine, but how do you know the status of the client? In other words, how do you know that the client is ready to receive?

Two sub-threads should be used, one is used to monitor and accept client connection requests, and add these requests to a list; the other thread keeps running Socket.Select to filter the Socket in the list to determine the status of the client, which is waiting Receive or wait to send. Then, use asynchronous receiving or sending to process the client's request.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list