C# Socket: how to keep it open?

2024/11/16 10:18:37

I am creating a simple server (C#) and client (python) that communicate using sockets.

The server create a

var listener = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp)

then binds, listens in a infinite loop

while (true)
{// Set the event to nonsignaled state.AllDone.Reset();// Start an asynchronous socket to listen for connections.Console.WriteLine("Waiting for a connection...");listener.BeginAccept(AcceptCallback, listener);// Wait until a connection is made before continuing.AllDone.WaitOne();
}

the AcceptCallback calls BeginReceive whose callbackk reads the buffer and sends back a message to the client

everything works. Client sends a message, server sees it, replies, client sees it. But when the client tries to send a message again (for testing client is in a loop), nothing happens

the python client:

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
while True:sock.sendall(data)blockLen = sock.recv(32) #server sends back a string prefixed with how long the string isserverReply = sock.recv(blockLen)

how do i keep the socket from dying?

Answer

this is from MSDN(Remarks on EndAccept):

Your callback method should invoke the EndAccept method. When yourapplication calls BeginAccept, the system usually uses a separatethread to execute the specified callback method and blocks onEndAccept until a pending connection is retrieved. EndAccept willreturn a new Socket object that you can use to send and receive datawith the remote host. You cannot use this returned Socket to acceptany additional connections from the connection queue. If you want theoriginal thread to block after you call the BeginAccept method, useWaitHandle.WaitOne. Call the Set method on a ManualResetEvent in thecallback method when you want the original thread to continueexecuting.

and there is another question on stackoverflow check.will it be helpful?

https://en.xdnf.cn/q/119541.html

Related Q&A

Python Selenium - how to get confirmation after submit

I have a follow up question on this post, I want to get any confirmation text after I hit submit button. Either the code works or not. html - invalid example <div class="serialModalArea js-seri…

Assigning a input on a line to its line number?

I was having a go at the following problem from the AIO (Australian Informatics Olympiad) Training Problems Site (Question in Italics and specifics in bold, my attempt below): The Problem Encyclopaedia…

Create a new list according to item value

I have a list like below. [T46, T43, R45, R44, B46, B43, L45, L44, C46, C45]where I want to group according to int value:[id][ , , , , ] # AREA PATTERN [Top, Right, Bottom, Left, Center][46][1,0,1…

Concatenating many time and date columns

I have many date and time column pairs (around 15 each) that share the same prefix, ie. SH or DEL. The columns are all of dtype object, ie. string. All the columns belong to the same Dataframe. Here is…

Python with ICS files and CSV [duplicate]

This question already has answers here:Parsing files (ics/ icalendar) using Python(6 answers)Closed 5 years ago.one friend ask me some help on a personnal project, but I have to admit my skills in Pyth…

loading my data in numpy genfromtxt get errors

I have my data file contain 7500 lines with :Y1C 1.53 -0.06 0.58 0.52 0.42 0.16 0.79 -0.6 -0.3 -0.78 -0.14 0.38 0.34 0.23 0.26 -1.8 -0.1 -0.17 0.3…

Install dlib with cuda support ubuntu 18.04

I have CUDA 9.0 and CUDNN 7.1 installed on Ubuntu 18.04(Linux mint 19). Tensorflow-gpu works fine on GPU(GTX 1080ti).Now i am trying to build dlib with CUDA support:sudo python3 setup.py install --yes …

View 3 dimensional Numpy array in Matplotlib and taking arguments from Keyboard or mouse

I have 3 dimensional data say (5,100,100). Now I would like to see them slice by slice upon hitting the down arrow button.

python default argument syntax error

I just wrote a small text class in python for a game written using pygame and for some reason my default arguments arent working. I tried looking at the python documentation to see if that might give m…

Variable not defined (Python)

FlightType=input("Which flight would you like to fly? Type 2 Seater, 4 Seater, or Historic.") # No validation included for the inputFlightLen=input("Would you like to book the 30 minu…