[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [CPROG] [c]oprette tcp socket i en funktion, skrive til den fraen anden



Hej

Kim Schulz wrote:

hejsa
Jeg sidder lige og debugger et lille program, og er stødt på et
underligt problem. Jeg har en tcp socket som bliver åbnet og der bliver
forbundet til en anden maskine. Dette sker i en funktion. Linjen holdes
åben. I en anden funktion skriver jeg så til samme socket via write. Socket fd er selvfølgelig defineret i global scope. Er ideeen ikke at man med sockets netop kan skrive(write) til en
forbindelse imens man også lytter (read) på den ?


Jo. En tcp-forbindelse (via en socket) er en full-duplex-forbindelse
du kan lase og skrive til.

Noget i stil med

   fd = socket(AF_INET, SOCK_STREAM,0);
   bzero(addr,sizeof(addr));
   addr.sin_family = AF_INET;
   addr.sin_addr.s_addr = htonl(YOUR_SERVER_ADDR);
   addr.sin_port = htons(port);
   // Bind is for listening, but here we ignore it
   // bind(fd, (struct sockaddr *) &addr, sizeof(addr));
   connect(fd, (struct sockaddr *) &addr, sizeof(addr));
   write(fd,"Hallo",5);
   read(fd,buf,BUFSIZ);

Hvis jeg ikke har glemt noget. Stevens bog har det hele med.
http://www.awprofessional.com/catalog/product.asp?product_id={F765FE89-50B3-4CEF-B315-8F9B748A7511}&selectDescTypeId={06B328CA-921B-4395-945D-3078CA6F292A}&st=5130B593-BAEC-49C6-B6A8-0035DFD1EA3B&session_id={D6123904-8A7C-4888-84B1-8B413028F569}
Det du laeser med read skulle alene vaere afhaengigt, hvad serveren i den anden ende sender til dig.


Her printer den i stedet det sendte til stdout.


Saa er der nok nogle pointere, der er overskrevet eller en fd du saetter forkert.


Er det mid der skal debugge lidt mere, eller er det virkeligt sådan at
jeg ikke kan skrive på den socket samtidigt med at jeg læser på den?


(læsefunktionen kører i en pthread)


Det kan man sagtens. Det er meningen med en Full-duplex linje. Threads betyder ikke noget i den sammenhaneg, hvis du goer det rigtigt.

--PMM




 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:10 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *