Berkeley CSUA MOTD:Entry 10702
Berkeley CSUA MOTD
 
WIKI | FAQ | Tech FAQ
http://csua.com/feed/
2025/05/25 [General] UID:1000 Activity:popular
5/25    

2003/10/20-21 [Computer/SW/Languages/C_Cplusplus, Computer/Networking] UID:10702 Activity:kinda low
10/20   I need to design some sort of tcp socket system for responding to
        client commands (sent from a gui).  The client will be sending
        text strings such as "set_foo_bar_baz=2340", but usually more complex.
        The server delegates the command to a specific function, which will
        respond with a potentially large (10Kb) response string.  Is it
        possible to pass the socket descriptor to the command handler s.t.
        the handler can fprintf() to the socket?  Is this advisable?  TIA.
        \_ What's wrong with passing the socket descriptor and using
           send()?
           \_ print formatting, ease of use, etc.  Also, it's a realtime
              system and we can only allocate memory at startup.
              \_ How can you have a realtime system rely on tcp?  Are you sure
                 it's a realtime system? -- ilyas
                 \_ The socket code is running in a low priority task which
                    talks to the RT task.  I'm interested in the file
                    descriptor solution because a dynamic malloc (even at
                    low-pri) might be too slow... the alternative is a purely
                    static buffer allocation, which I then pass into the
                    command handlers... but formatted printing into a char
                    array (sprintf; strcat) isn't quite as nice as fprintf.
                    \_ I think the tcp latency will dominate any latency from
                       a dynamic malloc.  Mallocs aren't that slow, compared
                       to a slow network.  Unless of course, your tcp is local.
                       Even in that case, the protocol makes no guarantees
                       about delivery times, so it would be difficult to
                       convince anyone your system is truly real time.
                         -- ilyas
                       \_ The RT data is coming in over the system bus; tcp
                          is only used for command & control.  I'm not
                          experienced enough to actually know what's going on,
                          but my mandate is that malloc is a no-no.  At any
                          rate, it looks like the static malloc decision has
                          already been made.  Thanks for your help though.
                 \_ Must be Linux based.
        \_ http://members.cox.net/defiant_penguin/documents/basic-socket.html
           There, have phun.
           \_ I think phun is depreciated.
                \_ Really?  Can I write it off on my taxes?  Or do
                   you really mean deprecated?
                        \_ just read the link retard.
2025/05/25 [General] UID:1000 Activity:popular
5/25    

You may also be interested in these entries...
2014/1/14-2/5 [Computer/SW/Languages/C_Cplusplus] UID:54763 Activity:nil
1/14    Why is NULL defined to be "0" in C++ instead of "((void *) 0)" like in
        C?  I have some overloaded functtions where one takes an integer
        parameter and the other a pointer parameter.  When I call it with
        "NULL", the compiler matches it with the integer version instead of
        the pointer version which is a problem.  Other funny effect is that
        sizeof(NULL) is different from sizeof(myPtr).  Thanks.
	...
2011/3/7-4/20 [Computer/SW/Languages/C_Cplusplus] UID:54056 Activity:nil
3/7     I have a C question.  I have the following source code in two identical
        files t.c and t.cpp:
                #include <stdlib.h>
                int main(int argc, char *argv[]) {
                  const char * const * p1;
                  const char * * p2;
	...
2009/7/21-24 [Computer/SW/Languages/Java] UID:53168 Activity:moderate
7/20    For those who care btw, it looks like eclipse is now A Standard Tool
        at UCB ugrad cs, probably replaced emacs.  Furthermore, people get
        angry at seeing Makefiles, (since eclispe takes care of that).  I
        guess it's just a sign of the times.
        \_ The more people at my work use eclipse the less the code is
           managable in emacs.  I'm not sure which application's fault
	...
2007/3/9-11 [Computer/SW/Languages/C_Cplusplus] UID:45917 Activity:nil 54%like:45865
3/9     When I start trn in a shell, I always get this:
        "*** glibc detected *** malloc(): memory corruption: 0x08091600 ***
        Abort"
        But when I run trn inside the shell buffer in emacs, the problem
        doesn't happen.  Any idea?  Thanks.
	...
2007/3/4-6 [Computer/SW/Languages/C_Cplusplus, Computer/SW/Mail, Computer/HW/Memory] UID:45865 Activity:nil 54%like:45917
3/4     trn crashes on me upon startup:
        "*** glibc detected *** malloc(): memory corruption: 0x08091618 ***
        Abort"
        Any idea?  Thanks.
	...
2006/1/25-27 [Computer/HW/Memory] UID:41522 Activity:nil
1/25    I'm working on a program in windows which is having memory problems.
        When I allocate memory with new or malloc, it seems to reserve more
        memory than I request (using VM Valiator to monitor this).  Even if I
        delete the memory there is still a "reserved" chunk that doesn't seem
        to ever get used by other code.  I'm using VC++ 6 btw.  Anyone have an
        idea of what might be going on?
	...
2005/9/9-13 [Computer/SW/OS/OsX] UID:39595 Activity:low
9/9     Does OSX gcc still not support weak symbol definitions?  ie, I
        can't redefine malloc or new? (In this case actually I'm defining
        something as weak, either with #pragma weak or
        __attribute__((weak)) but OSX doesn't seem to support either!)
        -jrleek
        \- hello, have you tried __attribute__((weak_import)). what gcc
	...
2005/6/24-27 [Computer/SW/SpamAssassin] UID:38285 Activity:nil
6/24    In my procmail log file, it says:
                csh in malloc(): warning: recursive call
                Out of memory.
        What is the meaning of this?
        \_ helps if you show us your .procmailrc
          \_ It's hundreds of lines long.  I was thinking it was
	...
2005/6/23-25 [Computer/SW/Languages/C_Cplusplus] UID:38255 Activity:low
6/22    Technical question:  My friend is trying to program a
        single-threaded signal based server.  He's using the icc compiler
        on 64-bit linux.  However, the following problem comes up.  The
        server is working and calls malloc.  Malloc grabs the allocation
        mutex (thread-safe!), then a signal comes in.  The signal
        interrupts malloc and calls the handler function.  The handler
	...
2005/2/15-16 [Computer/SW/Languages/C_Cplusplus, Computer/SW/Compilers] UID:36174 Activity:moderate
2/15    Technical question:  we have a memory leak in our C code, we think,
        but it's not the sort of memory leak where the memory's unreferenced.
        What we'd like to do is sort of a poor-man's profile, we want
        to know who calls our memory allocator "New"...  Sorta like a stack
        trace.  Using an actual profiler is sort of difficult 'cause it's
        a parallel application.  Thanks,  --peterm
	...
2005/1/24-26 [Computer/SW/Unix] UID:35874 Activity:low
1/24    How do I find out the maximum allowable process size on lesbians?
        \_ malloc
           \_ uh, how about finding out without getting squished?
        \_ Run "limit".
[ deleting bitch ]
           \_ limit is a csh thing.
	...
2004/12/8-9 [Computer/SW/Languages/C_Cplusplus, Computer/SW/Languages] UID:35217 Activity:kinda low
12/8    When people say null string or empty string in C, does it mean a char
        pointer that's NULL, or a char array whose first char is '\0'?  Thanks.
        \_ Both, because it's essentially the same. However, I think they
           probably mean the latter. I assume you mean a pointer pointing
           to NULL, and not a pointer which is NULL, which makes no real
           sense.
	...
2009/11/4-17 [Computer/SW/P2P, Computer/Networking, Computer/SW/Security] UID:53495 Activity:nil
11/4    Holy cow, I got a warning from my ISP that they were notified
        by BSA/baytsp.com that I was copying music/video/software.
        Do they do port scan or something? That's a first for me.
        \_ They hang out on P2P networks and track IP addresses.  -tom
           \_ I believe they are paid by content providers to perform this
              monitoring service, so you should only run this risk with content
	...
2008/11/7-13 [Computer/Networking] UID:51876 Activity:low
11/7    Need help on http proxy. After I VPN to work, I'd like to tunnel
        all the traffic to my machine. How do I setup my machine (Linux)
        as a proxy server so that my home computers can route through it?
        I'm asking because the site we're testing on requires that we
        come from the same IP. If I use VPN, the server will reject me
        based on the fact that it's a different IP than my work Linux.
	...
2008/8/5-10 [Computer/Networking] UID:50788 Activity:nil
8/5     It looks like my company has started blocking HTTPS tunneling.
        I used to do this by tunneling SSH through the HTTP/HTTPS proxy
        server, but this seems to have stopped working. Does anyone know
        how the implementation of tunneling detection works, and whether
        there are widely available implementations? We run a bunch of MS
        stuff, so I imagine we're running an MS proxy server or something.
	...
2007/6/28-7/2 [Computer/Networking] UID:47104 Activity:nil
6/28    what?
        We are deeply, deeply sorry to say that due to licensing constraints,
        we can no longer allow access to Pandora for most listeners located
        outside of the U.S. We will continue to work diligently to realize
        the vision of a truly global Pandora, but for the time being we are
        required to restrict its use. We are very sad to have to do this, but
	...
2006/10/31-11/2 [Computer/SW/OS/Windows] UID:45057 Activity:moderate
10/31   A friend of mine said he's loving Microsoft again because Bill G
        is starting to donate all of his money to charity. He's boycotting
        Google, Yahoo, and other mega companies because they're too big and
        too power and thinks they're all becoming the old Microsoft, whereas
        Microsoft has recently done a lot of good things like investing in
        education and charity. He just paid for a copy of Microsoft Windows
	...
2006/2/18-23 [Computer/Networking] UID:41923 Activity:low
2/18    My DSL modem's ip address is 192.168.0.1, my internal network
        behind my router is 10.0.0.x. Is there a way I can configure
        the router so I can access the DSL modem from my 10.0.0.x
        network directly without re-wiring? Static routes? I tried it
        but no much luck. I also tried changing my internal network to
        192.168.0.x, but still does not work. Thanks.
	...
2006/1/22-24 [Computer/Networking] UID:41477 Activity:nil
1/21    I am trying to setup a small network for my girlfriend's
        mom's company.  They just bought an accounting package
        which requires windows 2003 server.  And they want internet
        access from each computer.  How should the network be setuped?
        Would it be dumb to use static IP for each computer and a
        computer as internet gateway?
	...
2005/8/29-30 [Computer/Networking] UID:39329 Activity:moderate 54%like:37400
8/29    What's the difference between a hub, a switch and a router?  Thx.
        \_ AFAIK, probably be corrected by someone:
           hub: Allows communication on a LAN with bandwith shared amongs all
                the nodes on the hub and maxing out at the max line speed.
           switch: Allows communication on a LAN with bandwith greater than
                the max line speed (point to point)
	...
2005/6/2-3 [Computer/Networking] UID:37941 Activity:moderate
6/2     I've been to many places and almost every place I go to have
        802.11b/g. However, almost all of them have protected access,
        which I presume they use because they don't want people stealing
        their bandwidth. So here is one idea I think will really
        revolutionize 802.11X... an option in the router that allows you to
        specify the percentage of unprotected bandwidth you are willing to
	...
2005/2/25-27 [Computer/Networking] UID:36421 Activity:moderate
2/25    What is the smallest (physical and price) cisco router that can
        handle BGP?  It should be able to have more than 256 ram.
        \_ When you say ``handle BGP'', do you mean supports the bgp
           protocol or supports enough ram to keep a reasonable (what do you
           consider to be reasonable) number of routes in memory?  Do you want
           to be peering at PAIX, or do you just need a router to run the T1
	...
2005/1/13-14 [Computer/Networking] UID:35697 Activity:high
1/13    I need help fixing someone's Win2K box.  Setup:  Win2K box -> D-Link
        router -> DSL modem.  The Win2K box cannot obtain a DHCP address
        (other computers can).  So, I assign a static IP, and set the default
        gateway and DNS server to be the D-Link router.  After this, the Win2K
        box can access web pages on the Internet as long as you specify the
        web site IP address directly -- but DNS doesn't work.  Computer used
	...
Cache (5191 bytes)
members.cox.net/defiant_penguin/documents/basic-socket.html
I will try to go in depth to the reasoning behind all the strange formats to certain code, which will hopefully make it easier for you to understand and remember. It probably wont make a lot of sense for a while, because I am not going to give examples about why you do some stuff. You probably dont want to skip anything, because it is for the most part all necessary. All the code in this tutorial has been tested and compiled successfully with gcc on gnu/Linux. It should work fine on any of the Linuxs or BSDs, I dont know about anything else. You should be able to get most of this to work on microshit, but I will have no part in it. Note that the code in this tutorial does not have error checking, for clearity. I advise you to add your own error checking when you begin programming, because otherwise your program will just keep going and you wont even know there is an error. Conventions: include dependencies function and other definition code And include dependencies int main //Normal Code which does stuff, not just a definition return 0; Note how I use the s instead of the Greater-than / Less-than signs. If I dont explain something fully or you need some more data on it, please look in the man pages and on the Internet before consulting me. However, If you cant find it I would be happy to answer your question. Also, if you want some further advanced reading on this subject, I HIGHLY recommend the book UNIX Network Programming Volume I by Richard Stevens. Basic Stuff Chapter 1: Really Basic Stuff Okay first off, you need to know what a socket file descriptor sfd is. You open a socket, and you get a sfd back, and you use this sfd to connect to hosts, send data, and receive data. There should be already three socket file descriptors open on your machine. That is probably all you need to know about that for now, so onwards. This next one will take a brief history, although you probably already know this, it is necessary. When the socket interface was created, there were a bunch of different protocols competing to be the leading one. As we know today, the ones that were the major survivors of this contest were TCP and IP. However, when they were creating the socket interface, they had no idea who would win. So they cleverly invented a way so they could easily add more protocols, and people wouldnt have to learn a new interface. One of the most important structures in the socket interface is struct sockaddr. What struct sockaddr contains is the family more in a minute, a network address, and a port. Sockaddr is the generic struct they put in every function, however, to use the tcp and ip protocols, you must use the struct sockaddr_in. This is somewhat confusing to socket-newbies, but after a while it makes perfect sense. Another example of a structure you might use in here, is sockaddr_un. You wont be learning about this, but it is important to show a common example. So as you can see, it really is not that complicated to create a socket. Next up, you get to learn what to do with your spankin-new socket. Chapter 3: Connecting Okay, to use a socket in cool ways, you will have to connect it to a remote host, or bind it to a local port. The first section will focus on connecting, then in the end I will cover binding. Before you can do any of that, however, struct sockaddr_in needs further explanation. If that looks bafflingly hard, try going over line by line explaining to yourself what each line does. Dont get too attached to the ease of specifying an ip address as the s_addr. Chapter 4: Reading and Writing Ok, just one more chapter before you learn about format conversion and can actually build something. But you wouldnt really want to build something where you couldnt read and write to a socket, would you? First off, I will cover the read and write functions which are the easiest. Then I will cover using fdopen so you can use fprintf and fgets on sockets. I wont be explaining the use of sendto and recvfrom, mainly because those are functions which are primarily used for UDP and RAW sockets. Thats coming up next, and when youre done youll be able to read and write from sockets with ease. Format Conversion Chapter 1: HBO and NBO Okay, I should first explain what these acronyms stand for insert lame movie channel joke here. Different hosts use different byte orders, but networks always use the same byte ordering. So there are functions to take numbers in host byte order the one that looks right to you and put it in network byte order, and vice versa. Its easier to remember the names if you think of them as host to network short, host to network long, network to host short, and network to host long. Most of the time with these youll be using the short ones, to convert port numbers to network byte order. Final Project Chapter 1: tcp_connect Ok this chapter will give you an example of all of this put together. Next chapter will give you some ideas of projects you can do to gain understanding. This is a function I made and use regularly, it creates a socket and connects it to a certain host and port, to save space in the main functions. You can give it either an IP address or a domain name, and it will work.