Question: How can I update the code, so 2 people on different computers ( not terminals ) can communicate with each other? Thank you! client.c #include
How can I update the code, so people on different computers not terminals can communicate with each other? Thank you!
client.c
#include
#include
#include
#include
#include
#define PORT
#define BUFFERSIZE
int main
int sock ;
struct sockaddrin servaddr;
char bufferBUFFERSIZE;
Create socket
if sock socketAFINET, SOCKSTREAM,
printf
Socket creation error
;
return ;
servaddr.sinfamily AFINET;
servaddr.sinport htonsPORT;
Convert IP address to binary format
if inetptonAFINET, &servaddr.sinaddr
printf
Invalid address Address not supported
;
return ;
Connect to server
if connectsockstruct sockaddr &servaddr, sizeofservaddr
printf
Connection Failed
;
return ;
printfConnected to server!
;
while
Send message to server
printfYou: ;
fgetsbuffer BUFFERSIZE, stdin;
sendsock buffer, strlenbuffer;
Receive message from server
memsetbuffer BUFFERSIZE;
int valread readsock buffer, BUFFERSIZE;
if valread break;
printfServer: s
buffer;
closesock;
return ;
server.c
#include
#include
#include
#include
#include
#define PORT
#define BUFFERSIZE
int main
int serverfd newsocket;
struct sockaddrin address;
int addrlen sizeofaddress;
char bufferBUFFERSIZE;
Create socket
if serverfd socketAFINET, SOCKSTREAM,
perrorSocket failed";
exitEXITFAILURE;
Bind the socket to the network address and port
address.sinfamily AFINET;
address.sinaddr.saddr INADDRANY;
address.sinport htonsPORT;
if bindserverfdstruct sockaddr &address, sizeofaddress
perrorBind failed";
closeserverfd;
exitEXITFAILURE;
Listen for incoming connections
if listenserverfd
perrorListen failed";
closeserverfd;
exitEXITFAILURE;
printfWaiting for a connection...
;
Accept a connection
if newsocket acceptserverfdstruct sockaddr &address, socklent&addrlen
perrorAccept failed";
closeserverfd;
exitEXITFAILURE;
printfConnected to client!
;
while
Receive message from client
memsetbuffer BUFFERSIZE;
int valread readnewsocket, buffer, BUFFERSIZE;
if valread break;
printfClient: s
buffer;
Send message to client
printfYou: ;
fgetsbuffer BUFFERSIZE, stdin;
sendnewsocket, buffer, strlenbuffer;
closenewsocket;
closeserverfd;
return ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
