Download presentation
Presentation is loading. Please wait.
1
ECS152b Behrooz Khorashadi
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, struct sockaddr *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
9
Struct for Sockets struct sockaddr_in {
short sin_family; /* must be AF_INET */ u_short sin_port; struct in_addr sin_addr; char sin_zero[8]; /* Not used, must be zero */ }; Struct sockaddr{ usigned short sa_family; char some_data[14]
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.