Jump to content
Sign in to follow this  
Shuhamly

Selesaikan Soalan Nie

Recommended Posts

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);

}

else

fork();

}

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");

else

printf("received: %s\n",buf);

}

Share this post


Link to post
Share on other sites

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 address

s = socket (AF_INET, SOCK_STREAM, 0);

if ( s<0)

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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...

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...