1 / 9

Socket Tutorial

Socket Tutorial. ECS152b Behrooz Khorashadi. Don’t Forget. #include <sys/socket.h> #include <arpa/inet.h>. Socket Construction. int socket( int protocolFamily, int type, int protocol) int sockID = socket(PF_INET, SOCK_STREAM, 0); int sockID = socket(PF_INET, SOCK_DGRAM, 0);

billington
Download Presentation

Socket Tutorial

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Socket Tutorial ECS152b Behrooz Khorashadi

  2. Don’t Forget • #include <sys/socket.h> • #include <arpa/inet.h>

  3. Socket Construction • int socket(int protocolFamily, int type, int protocol) • int sockID = socket(PF_INET, SOCK_STREAM, 0); • int sockID = socket(PF_INET, SOCK_DGRAM, 0); • Return >0 or -1 for error • AF_INET or PF_INET

  4. Socket Connection • int connect(int socket, structsockaddr *serveraddress, unsigned address) • connect(sockID, &serv_addr, sizeof(serv_addr)) • serv_addr is actully of type sockaddr_in

  5. Socket Send • int send (int socket, const void * msg, unsigned int msglength, int flag) • int stringlength= strlen(stringUsend) • send(sockID, stringUsend, stringlength, 0) • Send will block until send has completed

  6. Socket Receive • int recv(int socket, void *rcvBuffer, unsigned int bufferLength, int flag) • Recv(sockID, recvBuffer, buffersize-1, 0) • recvBuffer is a char buffer you create of you own size. • You can use a while loop

  7. Socket Close • … close(sockID)… • This should be done on the client side.

  8. More Help • A good place to start Internet • http://www.cs.rpi.edu/courses/sysprog/sockets/sock.html

  9. Struct for Sockets • struct sockaddr_in { short sin_family; /* must be AF_INET */ u_short sin_port; structin_addr sin_addr; char sin_zero[8]; /* Not used, must be zero */ }; • Struct sockaddr{ usigned short sa_family; char some_data[14] };

More Related