Question:
what is the code or built in function in C to handle 1 client at a time by a server?
Muhammad Aqib
2013-04-15 20:58:58 UTC
what is code of sever for TCP that handle 1 client at a time?
Three answers:
Jared
2013-04-15 21:13:40 UTC
Creating a TCP client-server in C is fairly complicated and a lot of people use bad practices to accomplish it (like manually filling out the ip address struct). If you want to learn C socket programming that I HIGHLY recommend Beej's Guide to Network Programming (this is all freely available info on his webpage): http://beej.us/guide/bgnet/





Frankly, imo, C/C++ is a HORRIBLE language to do network programming in. Java is a MUCH MUCH better language for network applications. If all you want to do is TCP/UDP or even SSL sockets, then Java can do the job and it's much easier than in C. Now, if you want to do things at an even lower level than TCP/UDP, then C might be the way to go (i.e. sending raw IP datagrams).



Here are the problems with C/C++:



1) have to worry about different compilers. So a struct on your machine may very well be of different length than the SAME struct on another machine (it's compiler dependent). This means you must encapsulate your data yourself and make sure it's read and written the correct way.



2) You have to worry about byte order. So if you try to communicate between a Little Endian machine and a Big Endian machine, you are going to have to make sure that you first, send your data through hton (host to network) (which basically ensures your data is Big Endian), then on the reading side, send it through ntoh (network to host) to make sure the Big Endian network order gets converted to your machine (which may or may not do ANYTHING)...but you HAVE to do this to ensure you have functionality between machines that may or may not have different byte ordering.





Neither of these things are issues in Java:



1) All primitive types in Java have preset lengths. So an int is always 4 bytes, a short is always 2 bytes, etc. Furthermore, there are methods for writing these primitives to a datastream (i.e. the socket's datastream). You still need some form of encapsulation (i.e. if you want to transmit an object, then you will have to have writer and reader methods).



2) Java is ALWAYS Big Endian, regardless of your machine, so there is no need to worry about byte order with Java.



Edit:



I don't get at all what UnfaithFull is saying. I'm not going to argue that communicating with servers via TCP isn't going to be difficult and almost certainly not worth the trouble. However, if you are writing your OWN client/server (which is what I assumed) then this is just fine. Your only real obstacle with using TCP is adhering to the protocol. Again, I don't doubt that you should use software that is already written, but it's not impossible or even that difficult to do it yourself.



As an example, I'm fairly confident that I could write a C program that could get the HTML text from an HTTP server using nothing but low level TCP (i.e. http://www.google.com) (it would be 100s of times easier in Java, but it's doable in C)...





Edit:



Here is C code which uses low-level TCP sockets to connect to an HTTP server and retrieve data (it's 185 lines): http://ideone.com/HVl2wM



There is absolutely no error checking whatsoever in this code. It works because I specify a non-persistent connection, so I can just read the TCP data until I get an error (which should mean that the server closed the connection).



This program will retrieve the HTML text from whatever website you specify. I noticed that www.facebook.com didn't really work because it wanted you to redirect to an https URI so this might not get HTML for all websites (also it only gets the root page from the domain). So this is by no means a "robust" program, but it should show you how to use TCP to connect to a server and furthermore that you CAN connect to servers with low-level TCP connections (although, I'll still admit, it's cumbersome and other software should be capable of making it easier).





Just to give you some idea, here is the equivalent Java code: http://ideone.com/DVMbjD



Notice that this is 69 lines (that's counting the fact that the 8 import lines could have been condensed to 2, meaning this is about 1/3 of my C code). Notice how much simpler and more straightforward the Java code is. I'm sure if you talked to Python people that they would say they could do it in even fewer lines, but I still think Java is far better than C when it comes to network programming (nothing I did ever touched on the problems of different compilers or byte order...I was always sending char's which are always 1 byte in C).
Fred W
2013-04-15 21:09:44 UTC
You should research "inetd" for simple TCP servers. W Richard Stevens' book UNIX Network Programing contains very handy recipes for creating servers



http://www.kohala.com/start/ for his books



http://www.kohala.com/start/unp.html for source code
?
2016-11-03 18:47:15 UTC
ok.. the commmands... those are interpreted via you to the server.. i think you have some form of database keeping your costs, superb? nicely if the person varieties a command like "g:3".. it is your accountability to make sure what meaning.. perchance "g" stands for "get" and the colon shows a parameter is coming next.. and the enter of three is for the 0.33 quote.. as far as I comprehend what you're doing that's person-friendly manipulation of the database.. so which you've gotten saved technique calls and back you're those to make the mapping of instructions to the server.. if i knew greater of what those instructions have been meant to do i must be greater real in WHAT you choose to do.. -------- I pulled this out of the MS help on the thank you to establish sqlcommands and connections.. I comprehend your 'server app' is a console.. yet is it meant to drag from a db or something? Dim queryString As String = _ "choose OrderID, CustomerID FROM dbo.Orders;" '-- needless to say have your very own saved proc or line here.. making use of connection As New sq. Connection(connectionString) Dim command As New S qlCommand(queryString, connection) connection.Open() Dim reader As SqlDataReader = command.Exec uteReader() attempt whilst reader.examine() Console.WriteLine(String.type at("{0}, {a million}", _ reader(0), reader(a million))) end whilst ultimately ' consistently call close while accomplished examining. reader.close() end attempt end making use of


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...