Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How can I connect tcp server on one machine and tcp client on another one? Do I have to use a IP address? What are

How can I connect tcp server on one machine and tcp client on another one? Do I have to use a IP address? What are the steps to do that for mac and windows? PLEASE. IF YOU DON"T KNOW HOW TO DO IT DON'T DO IT. I WANT SOMEONE WHO KNOWS, PLEASE! THANK you! TCP server. /* server.c */ #include  #include  #include  #include  #include  #include  #include  #define SOCKET_ERROR -1 #define BUFFER_SIZE 100 #define MESSAGE "This is the message I'm sending back and forth" #define QUEUE_SIZE 5 int main(int argc, char* argv[]) { int hSocket, hServerSocket; /* handle to socket */ struct hostent* pHostInfo; /* holds info about a machine */ struct sockaddr_in Address; /* Internet socket address stuct */ int nAddressSize=sizeof(struct sockaddr_in); char pBuffer[BUFFER_SIZE]; int nHostPort; if(argc < 2) { printf(" Usage: server host-port "); return 0; } else { nHostPort=atoi(argv[1]); } printf(" Starting server"); printf(" Making socket"); /* make a socket */ hServerSocket=socket(AF_INET,SOCK_STREAM,0); if(hServerSocket == SOCKET_ERROR) { printf(" Could not make a socket "); return 0; } /* fill address struct */ Address.sin_addr.s_addr=INADDR_ANY; Address.sin_port=htons(nHostPort); Address.sin_family=AF_INET; printf(" Binding to port %d ",nHostPort); /* bind to a port */ if(bind(hServerSocket,(struct sockaddr*)&Address,sizeof(Address)) == SOCKET_ERROR) { printf(" Could not connect to host "); return 0; } /* get port number */ getsockname( hServerSocket, (struct sockaddr *) &Address,(socklen_t *)&nAddressSize); printf("opened socket as: fd (%d) on port (%d) for stream i/o ",hServerSocket, ntohs(Address.sin_port) ); printf("Server: \ sin_family = %d \ sin_addr.s_addr = %d \ sin_port = %d " , Address.sin_family , Address.sin_addr.s_addr , ntohs(Address.sin_port) ); printf(" Making a listen queue of %d elements",QUEUE_SIZE); /* establish listen queue */ if(listen(hServerSocket,QUEUE_SIZE) == SOCKET_ERROR) { printf(" Could not listen "); return 0; } for(;;) { printf(" Waiting for a connection "); /* get the connected socket */ hSocket=accept(hServerSocket,(struct sockaddr*)&Address,(socklen_t *)&nAddressSize); printf(" Got a connection"); strcpy(pBuffer,MESSAGE); printf(" Sending \"%s\" to client",pBuffer); /* number returned by read() and write() is the number of bytes ** read or written, with -1 being that an error occured ** write what we received back to the server */ write(hSocket,pBuffer,strlen(pBuffer)+1); /* read from socket into buffer */ read(hSocket,pBuffer,BUFFER_SIZE); if(strcmp(pBuffer,MESSAGE) == 0) printf(" The messages match"); else printf(" Something was changed in the message"); printf(" Closing the socket"); /* close socket */ if(close(hSocket) == SOCKET_ERROR) { printf(" Could not close socket "); return 0; } } } 

TCP client.

/* client.c */ #include  #include  #include  #include  #include  #include  #include  #define SOCKET_ERROR -1 #define BUFFER_SIZE 100 #define HOST_NAME_SIZE 255 int main(int argc, char* argv[]) { int hSocket; /* handle to socket */ struct hostent* pHostInfo; /* holds info about a machine */ struct sockaddr_in Address; /* Internet socket address stuct */ long nHostAddress; char pBuffer[BUFFER_SIZE]; unsigned nReadAmount; char strHostName[HOST_NAME_SIZE]; int nHostPort; if(argc < 3) { printf(" Usage: client host-name host-port "); return 0; } else { strcpy(strHostName,argv[1]); nHostPort=atoi(argv[2]); } printf(" Making a socket"); /* make a socket */ hSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); if(hSocket == SOCKET_ERROR) { printf(" Could not make a socket "); return 0; } /* get IP address from name */ pHostInfo=gethostbyname(strHostName); /* copy address into long */ memcpy(&nHostAddress,pHostInfo->h_addr,pHostInfo->h_length); /* fill address struct */ Address.sin_addr.s_addr=nHostAddress; Address.sin_port=htons(nHostPort); Address.sin_family=AF_INET; printf(" Connecting to %s on port %d",strHostName,nHostPort); /* connect to host */ if(connect(hSocket,(struct sockaddr*)&Address,sizeof(Address)) == SOCKET_ERROR) { printf(" Could not connect to host "); return 0; } /* read from socket into buffer ** number returned by read() and write() is the number of bytes ** read or written, with -1 being that an error occured */ nReadAmount=read(hSocket,pBuffer,BUFFER_SIZE); printf(" Received \"%s\" from server ",pBuffer); /* write what we received back to the server */ write(hSocket,pBuffer,nReadAmount); printf(" Writing \"%s\" to server",pBuffer); printf(" Closing socket "); /* close socket */ if(close(hSocket) == SOCKET_ERROR) { printf(" Could not close socket "); return 0; } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database Foundations Technology Fundamentals For IT Success

Authors: Bob Bryla

1st Edition

0782143725, 9780782143720

More Books

Students also viewed these Databases questions