sox.cpp
//sockets
#include <cstring>
#include <iostream>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <cstdlib>
#include <arpa/inet.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(){
//server id it will only be receiving code from the client
int sockfd,incomingfd;
sockaddr_in serveraddr;
sockaddr_in clientaddr;
sockfd=socket(AF_INET,SOCK_STREAM,0);//scoekt
bzero((char *) &serveraddr, sizeof(serveraddr));//set to zero
//for storing sent/received message
char message[256];
message[0]='x';
//set serveraddr
serveraddr.sin_family=AF_INET;
serveraddr.sin_port=htons(2500);
serveraddr.sin_addr.s_addr=INADDR_ANY;//let the OS manage this other wise the nodes addrr
//ok
//bind the socket to the server address
int status=bind(sockfd,(sockaddr *) &serveraddr,sizeof(serveraddr));
if(status<0){
std::cout<<"OOPS FAILED TO BIND";exit;}
else std::cout<<"Server is alive\n";
//end of condition
std::cout<<"Awaiting for incoming connections\n";
listen(sockfd,5);
socklen_t client_len=sizeof(clientaddr);//get socket length
if(incomingfd=accept(sockfd,(sockaddr*)&clientaddr,&client_len)>0){
std::cout<<"connection made to a device\n";}//end of if
if(incomingfd>0){
while(1==1){
bzero(message,256);
int n=read(incomingfd,message,255);
if(n<0)std::cout<<"Fail to read\n";
std::cout<<"user says "<<message<<std::endl;
}//the infinte input reading loop
}//end of if
std::cin.get();
return 0;
}
user.cpp
#include <iostream>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <cstdlib>
#include <cstring>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(){
sockaddr_in host;
bzero((char *) &host, sizeof(host));
int hostfd;
int sockfd;
int port=2500;
char message[256];
//create a socket
sockfd=socket(AF_INET,SOCK_STREAM,0);
//initialize host
host.sin_family=AF_INET;
host.sin_port=htons(port);
host.sin_addr.s_addr=inet_addr("192.168.0.103");
//connect to host
std::cout<<"Connecting to host 192.168.0.3"<<std::endl;
if(connect(sockfd,(sockaddr*)&host,sizeof(host))<0){std::cout<<"Failed to connect\n";exit;}
else{
std::cout<<"Connected to the host successfully\n";
while(true)
{
std::cout<<"Enter your message to send to the server\n";
bzero(message,256);
std::cin>>message;
std::cout<<"Okay sending messaging to the server\n";
int n=write(sockfd,message,strlen(message));
if(n<0){std::cout<<"Error writing ";exit;}
else std::cout<<"Your message is sent \n";
}//end of while body
}//heres the main loop for keep sending messages lel
std::cin.get();
return 0;
}
I am trying to have one end(user) send messages and other end(server) reading them and just writing them down. The user.cpp is working fine it keeps asking you to enter a message after establishing a connection.
The server.cpp works fine too to the point of establishing connection (it tells the connection is made once you run user.exe / user.o) but it won't write the received message to console. i am sure it is receiving the messages because it would've generated an error other wise because of the line
if(n<0)std::cout<<"Fail to read\n";
But it's not, it just keeps wiating and wiating and prints no message ;/ i don't have C background so I could've fucked up somewhere