c - How to enter an IP address manually in Socket Programming and how to print traces? -
i working on project have program client/server have test connection , send/receive files , list files saved on server.
i on first step , , asked enter manually port number , ip address , blocked on ip address declaration..
there code
void connection(){ int port; int sockfd,connfd; struct sockaddr_in servaddr,cli; sockfd=socket(af_inet,sock_stream,0); if(sockfd==-1) { printf("socket creation failed...\n"); exit(0); } else printf("socket created..\n"); printf("enter port number \n"); scanf("%d",&port); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=af_inet; servaddr.sin_addr.s_addr=inet_addr("127.0.0.1"); servaddr.sin_port=htons(port); if(connect(sockfd,(sa *)&servaddr,sizeof(servaddr))!=0) { printf("connection server failed...\n"); printf("program closed\n"); exit(0); } else printf("connected server..\n"); printf("listening server\n"); func(sockfd); close(sockfd);} now if want declare variable , put ip "127.0.0.1" in , use in line servaddr.sin_addr.s_addr=inet_addr ?
and after establishing connection server, able send , receieve messages , want show traces (frames) size in bytes etc. idea?
thank you
it simpler part variable ip. can use char array fixed size 16 avoid dynamic memory allocation, since 16 bytes enough store string valid ipv4 address (for example "255.255.255.255" - 3 dots + 12 digits + 1 null symbol terminating string).
the code reading ip:
char ip[16]; scanf("%15s", ip); /* if needed possible write function validation of * enetered string check valid ip address string: 4 decimal * numbers in range 0...255 separated dots */ /* ... */ servaddr.sin_addr.s_addr = inet_addr(ip); the string "%15s" in scanf means want read null terminated string s , lenght should maximum 15 characters (+1 null not counted in number).
regarding frames @ first though want capture separate packets. however, looks want reguaral client-server communation using tcp sockets prints activities.
let's consider client example. have connected state. next should prepare data buffer send. before sending can print "transmission of frame...".
then should prepare timeout. here how set socket timeout in c when making multiple connections? (the answer https://stackoverflow.com/a/4182564/4023446). can print "activation of timeout 1000ms".
data sending send(). function returns number of sent bytes. so, if number equal number of data sending possible print "sent".
similarly possible describe read operations on server phrases according stages: connected, reading (also possible timeout), received: "test".
Comments
Post a Comment