Shuhamly 0 Report post Posted March 4, 2005 actually aku dah tanya kat forum linux..pastu kena suru tanya kat forum nie plak..so kepada pakar2 C programming silakan...minta bantuan..soalan dia : Change the server and client program so that it will only accept connection from client with a certain IP address.(one IP address or a range of IP address)TCP Server Code.#include <sys/types.h>#include <sys/socket.h>#include<netinet/in.h>#include<arpa/inet.h>#include <stdio.h>int s;int s1;int rc;char buf [100];void mylisten(void);void mysend(void);int myreceive(void);void myaccept(void);int main (void){struct sockaddr_in local;int a;local.sin_family = AF_INET;local.sin_port = htons (7500);local.sin_addr.s_addr = htonl (INADDR_ANY);s = socket (AF_INET, SOCK_STREAM, 0);if ( s<0){perror ("socket call failed");exit(1);}rc = bind (s, (struct sockaddr *) & local, sizeof (local));if (rc<0 ){perror ("bind call failure");exit(1);}mylisten();myaccept();while(1){a = myreceive();if(!a)myaccept();}}void mylisten(){rc=listen(s, 5);if (rc){perror("listen call failed");exit(1);}elsefork();}void myaccept(){s1=accept (s, NULL, NULL);if (s1<0){perror("accept call failed");exit(1);}}int myreceive(){rc=recv( s1, buf, sizeof(buf), 0);if (rc<=0){perror("recv call failed");return 0;exit(1);}printf("%s\n", buf);mysend();}void mysend(){rc=send( s1, "2", 1, 0);if (rc<=0)perror("send call failed");}TCP Client Code:#include<sys/types.h>#include<sys/socket.h>#include<netinet/in.h>#include<arpa/inet.h>#include<stdio.h>int s;int rc;char buf[1];struct sockaddr_in peer;void mysend(char []);void myreceive(void);void myconnect();int main(void){char msg[100];peer.sin_family=AF_INET;peer.sin_port = htons(7500);peer.sin_addr.s_addr=inet_addr("127.0.0.1");s=socket(AF_INET,SOCK_STREAM,0);if(s<0){ perror("Socket call fail");exit(1);}myconnect(); while(1){ scanf("%s",msg);mysend(msg);myreceive();}exit(0);}void myconnect(){rc=connect(s,(struct sockaddr*)&peer,sizeof(peer));if(rc){perror("Connect call fail");exit(1);}}void mysend(char msg[]){rc=send(s,msg,100,0);printf("sent: %s\n",msg);if(rc<=0){ perror("Send call fail");exit(1);}}void myreceive(){rc=recv(s,buf,1,0);if(rc<=0)perror("recv call fail");elseprintf("received: %s\n",buf);} Quote Share this post Link to post Share on other sites
Shuhamly 0 Report post Posted March 4, 2005 possible x kalau aku letak mcm nie kat coding server...aku just letak half dr coding k! (setahu aku coding tu just tukar kat server je kan?? client tak yah)b4 tukar..int main (void){struct sockaddr_in local;int a;local.sin_family = AF_INET;local.sin_port = htons (7500);local.sin_addr.s_addr = htonl (INADDR_ANY);s = socket (AF_INET, SOCK_STREAM, 0);if ( s<0)afta tukar..int main (void){struct sockaddr_in local;int a;local.sin_family = AF_INET;local.sin_port = htons (7500);local.sin_addr.s_addr = inet_addr(10.0.3.2);//client IP addresss = socket (AF_INET, SOCK_STREAM, 0);if ( s<0) Quote Share this post Link to post Share on other sites
mohdfadly 24 Report post Posted March 4, 2005 yup, kalau nak accept certain IP, kena tetapkan address kat server je, kat client takyah.ataupun kalau ko ada lebih dari satu IP, bleh letakkan list of IP address kat dalam satu file.txt pastu buat array.* aku buat 2 copy topik ni ke dalam C, C++. C#. Bleh tanya lebih lanjut camner nak buat array. Quote Share this post Link to post Share on other sites
Shuhamly 0 Report post Posted March 4, 2005 actually aku akan run kan program kan nie kat linux(mandrake) so aku tak berapa familiar linux..tu la ada masalah sket...ada sepa2 leh tukar ke array tak?? maskud aku bg contoh coding jer... Quote Share this post Link to post Share on other sites