|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Linux socket programming
Where can I find a good introduction to socket programming?
|
|
#2
|
||||
|
||||
|
http://docs.sun.com/db/doc/802-5886...=sockets&a=view
... though this is for Sun, it should be largely the same ...
__________________
Jon Sagara "Me fail English? That's unpossible!" |
|
#3
|
||||
|
||||
|
__________________
"A poor programmer is he who blames his tools." http://analyser.oli.tudelft.nl/ |
|
#4
|
||||
|
||||
|
Try out Unix Network Programming by W. Richard Stevens. This is probably the Bible of Network Programming and reading just Volume I will probably make you more knowledgable about sockets than most other books. It covers socket programming very comprehensively and practically all network programming books and articles always have this book in their list of references.
|
|
#5
|
|||
|
|||
|
Linux socket programming
Thanks people,
I have been trying to understand the info on the web about sockets, threading and multiplexing. I find that jumping around from site to site can be too much information to take in at once. I will visit the links and buy the book... Thanks again. |
|
#6
|
||||
|
||||
|
Stevens' "Unix Network Programming" is indeed considered the "Bible of Network Programming", but if you are looking for an introduction then maybe a "tract" would be more useful at this point. Stevens may prove to be too comprehensive for you right now.
As an introduction, I would recommend "TCP/IP Sockets in C: Practical Guide for Programmers" by Michael J. Donahoo and Kenneth L. Calvert, Morgan Kaufmann Publishers, 2001 (ISBN 1-55860-826-5). It's 110 pages long and costs $15. OTOH, "Unix Network Programming" comes in two volumes at about $70 each, $140 total -- not even Amazon.com offers a discount -- and appears to cover all of POSIX interprocess communication as well as a non-sockets API called XTI. The previous edition of "TCP/IP Sockets in C: Practical Guide for Programmers" was called "The Pocket Guide to TCP/IP Sockets: C Version" (ISBN 1-55860-686-6) and is still out in the bookstores and covers pretty much the same material. I started out studying and researching all I could and found myself in "analysis paralysis", afraid to try to write a sockets program until I understood it all. "The Pocket Guide to TCP/IP Sockets: C Version" got me coding almost immediately. So far in my spare time, I've written time clients and servers, an embedded telnet client, and a "man in the middle" utility that I used to capture and analyze telnet traffic (for writing the embedded client). Their books (they've also written one for Java) are a good introduction to sockets programming that can get you started fast. It also has a good API reference section in back which I keep in my Palm for reference. You should also visit their web sites: http://cs.baylor.edu/~donahoo/PocketSocket/ and http://cs.baylor.edu/~donahoo/practical/CSockets/ for the source code and for information on writing Winsock applications. It turns out that a Unix sockets app can be converted to Windows with just a few minor changes (a common task for me, since I'm the only one at work with a Linux box); the process is described in a short document on their site, "Transitioning from UNIX to Windows Socket Programming" by Paul O'Steen (http://cs.baylor.edu/~donahoo/pract...dowsSockets.pdf ). Then after you have gotten a good introductory start, Stevens would be more useful. Also, since you named this thread "Linux Socket Programming", you might want to look at Warren Gay's "Linux Socket Programming by Example" (ISBN 0-7897-2241-0). It starts with the basics and moves on to more advanced topics, like . For example, I was able to verify my design for a broadcast client application, which I have not seen covered elsewhere. Also, you might want to check out Analyser's "shameless plug". I found Beej's Guide to be helpful. BTW, sockets are the easy part. Working out the interaction between servers and clients are slight more difficult. The difficult part is researching the application-layer protocols, like telnet. For that, you will need to start digging through the Requests for Comment (RFCs). Good luck and have fun! |
|
#7
|
|||
|
|||
|
Linux Socket UDP C Program
Hi,
I am trying to develop an UDP socket C program in SUSE Linux. I have found and downloaded the UDP C program from Beej's Guide to Network Programming. I have compiled both the talker.c and listener.c in the SUSE Linux. Run the talker program in my MAC machine to send 14 bytes of data. And run the listener program in the SUSE Linux. However, I am not able to receive anything in my Linux machine. When doing the reverse, running the talker in the Linux machine; and running listener in the MAC machine, I can receive in my MAC machine. The code for the listener.c is shown below. Can someone tell me what is going wrong? I have been trying very hard and tried to find solution in the internet but still no clue. Really appreciate if someone can tell me what I have done wrong. Thanks. ///////////////////////////////////////////////////// /* ** listener.c -- a datagram sockets "server" demo */ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #define MYPORT "4950" // the port users will be connecting to #define MAXBUFLEN 100 // get sockaddr, IPv4 or IPv6: void *get_in_addr(struct sockaddr *sa) { if (sa->sa_family == AF_INET) { return &(((struct sockaddr_in*)sa)->sin_addr); } return &(((struct sockaddr_in6*)sa)->sin6_addr); } int main(void) { int sockfd; struct addrinfo hints, *servinfo, *p; int rv; int numbytes; struct sockaddr_storage their_addr; char buf[MAXBUFLEN]; size_t addr_len; char s[INET6_ADDRSTRLEN]; memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; // set to AF_INET to force IPv4 hints.ai_socktype = SOCK_DGRAM; hints.ai_flags = AI_PASSIVE; // use my IP if ((rv = getaddrinfo(NULL, MYPORT, &hints, &servinfo)) != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); return 1; } // loop through all the results and bind to the first we can for(p = servinfo; p != NULL; p = p->ai_next) { if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { perror("listener: socket"); continue; } if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) { close(sockfd); perror("listener: bind"); continue; } break; } if (p == NULL) { fprintf(stderr, "listener: failed to bind socket\n"); return 2; } freeaddrinfo(servinfo); printf("listener: waiting to recvfrom...\n"); addr_len = sizeof their_addr; if ((numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1 , 0, (struct sockaddr *)&their_addr, &addr_len)) == -1) { perror("recvfrom"); exit(1); } printf("listener: got packet from %s\n", inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *)&their_addr), s, sizeof s)); printf("listener: packet is %d bytes long\n", numbytes); buf[numbytes] = '\0'; printf("listener: packet contains \"%s\"\n", buf); close(sockfd); return 0; } |
|
#8
|
||||
|
||||
|
Firstread this (really, follow the link).
DO NOT BOTHER TO RESPOND TO THIS THREAD. Check that your local firewalls aren't preventing communications in one direction or the other. If that isn't the problem, start your own thread.
__________________
My worst nightmare was a pointless infinite loop. Work in progress; don't poke the curmudgeon! http://www.odonahue.com/ |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Linux socket programming |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|