Connection refused TCP sockets - c

I made a simple client and a simple server with TCP sockets.When I was testing them, I got an error message: Connection refused.
I have opened the ports for the server, so I don't understand why I get this error... Can you help me?
This is the client's source
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <errno.h>
#define PORTA 3459
int main()
{
char indirizzo[15];
char buffer[20];
struct sockaddr_in client;
int clients;
puts("Inserire l'indirizzo");
fgets(indirizzo, 15, stdin);
printf("L'indirizzo del destinatario è %s",indirizzo);
puts("Inserire il messaggio");
fgets(buffer, 20, stdin);
client.sin_family = AF_INET;
client.sin_port = htons(PORTA);
client.sin_addr.s_addr = inet_addr(indirizzo);
memset(client.sin_zero, '\0',8);
if((clients = socket(PF_INET, SOCK_STREAM,0)) == -1)
{
printf("%s\n", strerror(errno));
exit(0);
}
if( (connect(clients, (struct sockaddr *)&client, sizeof(structsockaddr)) ) == -1)
{
printf("%s\n", strerror(errno));
exit(0);
}
send(clients, buffer, 20,0);
close(clients);
return 0;
}
This is the server's source code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <errno.h>
#define PORTA 3459
int main()
{
struct sockaddr_in sock;
int socks;
struct sockaddr_in newsock;
int newsocks;
if((socks = socket(PF_INET, SOCK_STREAM,0)) == -1)
{
puts("Errore: socks non inizializzato\n");
printf("%s\n", strerror(errno));
exit(0);
}
sock.sin_family = AF_INET;
sock.sin_port = htons(PORTA);
sock.sin_addr.s_addr = htonl(INADDR_ANY);
memset(sock.sin_zero, '\0',8);
int si = 1;
if(setsockopt(socks,SOL_SOCKET,SO_REUSEADDR,&si,sizeof(int)) == -1)
{
puts("Errore durante il settaggio del socket\n");
printf("%s\n", strerror(errno));
exit(0);
}
if(bind(socks,(struct sockaddr *)&sock, sizeof(struct sockaddr) ) == -1)
{
puts("Errore durante il binding\n");
printf("%s\n", strerror(errno));
exit(0);
}
char buffer[30];
int lung;
lung = sizeof(newsock);
listen(socks, 5);
if((newsocks = accept(socks,(struct sockaddr *)&newsock,&lung)) == -1)
{
puts("Errore durante l'accettazione del socket remoto\n");
printf("%s\n", strerror(errno));
exit(0);
}
if(recv(newsocks, buffer,sizeof(buffer),0) == -1)
{
puts("Errore durante la ricezione dei dati");
printf("%s\n", strerror(errno));
}
puts(buffer);
return 0;
}

Code looks good in general, except for not controlling errors in some cases (inet_addr) and the ports are the same both in client and server. If you get connetion refused error, it cannot be a firewall-related problem.
Do you have the same error always or sometimes it works OK?
Most probably the problem is one of these:
1-Client is trying to connect to the wrong IP address. Check the address printed and result of inet_addr.
2-The server doesn't have a loop to keep listening for connections. It finishes after getting one connection. Maybe a first test works then server ends and client is run again getting connection refused.
You could also try not setting SO_REUSEADDR. I don't think it's related but if nothing else works ...

Related

I am trying to echo the string from the server to the client with UDP sockets but it is failing?

I have made one server and one client communicating through UDP sockets. The work that I am trying to do is that client will pass a string in the arguments and that string will be send to the server using UDP sockets. After receiving the string server will again echo(send) the string back to the client.Below are the codes for both:
code for echoClient.c :
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define MAXLINE 4096
int main(int argc, char const *argv[])
{
int sockfd;
struct sockaddr_in servAddr;
char sendLine[MAXLINE],recvLine[MAXLINE];
if(argc!=3)
{
printf("echoClient <Ip addr. of the server> <String to be echoed> \n");
exit(1);
}
if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0)
{
printf("Error in creating the socket\n");
exit(2);
}
memset(&servAddr,0,sizeof(servAddr));
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(6565); // setting up the port
servAddr.sin_addr.s_addr = inet_addr(argv[1]); // using the given ip address of the server
printf("%s\n", argv[2]);
if((sendto(sockfd,(const char *)argv[2],strlen(argv[2]), MSG_CONFIRM,(struct sockaddr *) &servAddr, sizeof(servAddr))!=-1))
{
printf("data is sent to the server\n");
}
else
{
printf("can't send the data to the server\n");
exit(3);
}
int n = recvfrom(sockfd,(char * ) recvLine,MAXLINE,0,(struct sockaddr * )&servAddr,sizeof(servAddr));
if(n==-1)
{
printf("Can't receive the data from the server\n");
exit(4);
}
recvLine[n] = '\0'; // to terminate the received string
printf("%s\n",recvLine);
return 0;
}
code for echoServer.c:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define MAXLINE 4096
int main(int argc, char const *argv[])
{
int sockfd;
struct sockaddr_in servAddr,clientAddr;
char sendLine[MAXLINE],recvLine[MAXLINE];
if(argc!=1)
{
printf("echoServer\n");
exit(1);
}
if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0)
{
printf("Error in creating the socket\n");
exit(2);
}
memset(&servAddr,0,sizeof(servAddr));
memset(&clientAddr,0,sizeof(clientAddr));
// filling the details of the server ip and port
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(6565);
servAddr.sin_addr.s_addr = INADDR_ANY; // using the given ip address of the server
if(bind(sockfd,(struct sockaddr * )&servAddr,sizeof(servAddr))<0)
{
printf("Binding of the socket failed\n");
exit(1);
}
printf("Server is Up... Waiting for the client...\n");
int len;
int n = recvfrom(sockfd,(char *) recvLine,MAXLINE,MSG_WAITALL,(struct sockaddr * )&clientAddr,&len);
if(n==-1)
{
printf("can't get the message from the client\n");
exit(2);
}
recvLine[n] = '\0';
printf("Message received from the client is %s\n",recvLine);
if(sendto(sockfd,(char *) recvLine,n,MSG_CONFIRM,(struct sockaddr *)&clientAddr,len)<0)
{
printf("can't send the message to the client\n");
exit(3);
}
return 0;
}
Now the actual problen is that when I am executing the above codes client is able to send the string to the server but server is unable to send the string back to the client.Server gives the error can't send the message to the client.
I am not able to figure out the error which is stopping the server to send the message to the client.Please help me with this.
I am running the echoClient.c with the command :
./a.out 127.0.0.1 hellofromclientside
In the server, you overlooked that the argument len to recvfrom() is a value-result argument, which before the call you have to initialize to the size of the clientAddr in order to get this address, so change
int len;
to
int len = sizeof clientAddr;
Similarly in the client, change
int n = recvfrom(sockfd,(char * ) recvLine,MAXLINE,0,(struct sockaddr * )&servAddr,sizeof(servAddr));
to
int len = sizeof servAddr;
int n = recvfrom(sockfd, recvLine, MAXLINE, 0, (struct sockaddr *)&servAddr, &len);

C - Sockets. client: connect: No such file or directory

I'm trying to set up a socket connection between a client and server where server creates socket and reads from it and client writes data to a server. Here is my code:
//server.c
/* a server in the unix domain.*/
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/un.h>
#include <stdio.h>
#define SOCKETNAME "mynewsocket"
int main(void){
char buffer[1024];
int n, sock , nsock;
socklen_t len;
struct sockaddr_un name;
if((sock = socket(AF_UNIX, SOCK_STREAM, 0))<0){
perror("server: socket");
exit(1);
}
memset(&name, 0, sizeof(struct sockaddr_un));
name.sun_family = AF_UNIX;
strcpy(name.sun_path, SOCKETNAME);
len = sizeof(name.sun_family) + strlen(name.sun_path);
unlink ( name.sun_path ) ;
if (bind(sock, (struct sockaddr *) &name, SUN_LEN(&name)) < 0) {
perror("server: bind");
exit(1);
}
if (listen(sock, 5) < 0) {
perror("server: listen");
exit(1);
}
if ((nsock = accept(sock, (struct sockaddr *) &name, &len)) < 0) {
perror("server: accept");
exit(1);
}
n=read(nsock,buffer,80);
printf("A connection has been established\n");
write(1,buffer,n);
write(nsock,"I got your message\n",19);
close(nsock);
close(sock);
exit(0);
}
and client...
//client.c
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/un.h>
#include <stdio.h>
#define SOCKETNAME "mynewsocket"
int main(void){
char buffer [1024];
int n, sock, len;
struct sockaddr_un name;
if((sock = socket(AF_UNIX, SOCK_STREAM, 0))<0){
perror("client: socket");
exit(1);
}
memset(&name, 0, sizeof(struct sockaddr_un));
name. sun_family = AF_UNIX;
strcpy(name.sun_path, SOCKETNAME);
len = sizeof(name.sun_family) + strlen(name.sun_path);
unlink ( name.sun_path ) ;
if (connect(sock, (struct sockaddr *) &name, SUN_LEN(&name)) < 0) {
perror("client: connect");
exit(1);
}
printf("Please enter your message: ");
bzero(buffer,82);
fgets(buffer,80,stdin);
write(sock,buffer,strlen(buffer));
n=read(sock,buffer,80);
printf("The return message was\n");
write(1,buffer,n);
close(sock);
exit(0);
}
To run the two programs, compile first and run it like this:
./server &
./client
When I run the two programs, the server executes fine but the client executes with an error saying No such file or directory
Why is that? What is wrong with my code?
unlink ( name.sun_path ) ;
Why do you unlink? This is the rendez-vous point for client&server! The server could create this file on the file system once it starts running , and unlink it when it exits. (but that is not needed)
The client uses the file to find the server. (te "secret" they share is the location and name of the socket-file.
AF_UNIX uses the file system as a namespace(filenames instead of ip-addresses+portnumbers) Internally, (most probably) the {dev_id,inodenumber} are used as "key" to identify the socket/file-descriptor

Connection refused error in socket programming

This code is generating "Connection Failed error", (the error generating portion is commented below in the code) even when i am supplying the correct input format eg.
./Client ip text portno
./Client 127.0.0.1 "tushar" 7100
//AUTHOR: TUSHAR MAROO
//Client.c
//header files used
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <netinet/in.h>
//constants
#define RCVBUFFERSIZE 32
//functions used
void DieWithError(char *errorMessage);
//main program
int main(int argc, char *argv[]){
int sock;
struct sockaddr_in serverAddr;
unsigned short serverPort;
char *serverIp;
char *message;
unsigned int messageLength;
char buffer[RCVBUFFERSIZE];
//condition check deplyed for nuber of arguements not for data in arguements
if((argc<3) || (argc>4)){
fprintf(stderr,"Format: %s <Server's IP> <Your Message> <Port Number>\n",argv[0]);
exit(1);
}
serverIp = argv[1];
message = argv[2];
if(argc == 4){
serverPort = atoi(argv[3]);
} else {
serverPort = 7;
}
//create a socket and check success and handle error
if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0 )
fprintf(stderr, "Socket Creation Fail");
//server details
//bzero((struct sockaddr_in *)(&serverAddr),sizeof(serverAddr));
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = inet_addr(serverIp);
serverAddr.sin_port = htons(serverPort);
printf("tusharmaroo");
//not working why??
//if (connect(sock, (struct sockaddr *) &serverAddr, sizeof(serverAddr)) < 0)
//DieWithError("Connection Error..");
//fprintf(stderr,"Connection error");
//this snippet also not working
if (connect(sock, (struct sockaddr *) &serverAddr, sizeof(serverAddr)) < 0)
DieWithError("connect() failed");
printf("connected....");
messageLength = strlen(message);
if(send(sock, message, messageLength, 0) > 0)
printf("message sent....");
close(sock);
exit(0);
}
//AUTHOR TUSHAR MAROO
//SERVER CODE
//header files
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>
//constants declared
#define ALLOWEDCONNECTIONS 5
//external functions
void DieWithError(char *error);
void ClientHandle(int sock);
//main code
int main(int argc, char argv[]){
int serverSock;
int clientSock;
struct sockaddr_in serverAddr;
struct sockaddr_in clientAddr;
unsigned int serverPort;
unsigned int clientLength;
if(argc != 2){
fprintf(stderr,"Format: %d <Port No.>", argv[0]);
//DieWithError("Pass Correct Number of Arguements...");
exit(1);
}
if((serverSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){
DieWithError("Socket not Created");
exit(1);
}
serverPort = htons((argv[1]));
//assign address to the server
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddr.sin_port = htons(serverPort);
//socket has been created now bind it to some ip and port
if((bind(serverSock,(struct sockaddr *)&serverAddr,sizeof(serverAddr))) < 0){
DieWithError("Binding Failed");
}
if(listen(serverSock,5) < 0){
DieWithError("Listen Failed");
}
for(;;){
clientLength = sizeof(clientAddr);
if((clientSock = accept(serverSock, (struct sockaddr *) &clientAddr, &clientLength)) < 0){
DieWithError("Accept() failed");
exit(1);
}
printf("Handling Client %s ",inet_ntoa(clientAddr.sin_addr));
}
return 0;
}
This is wrong in the server code
serverPort = htons((argv[1]));
This should be
serverPort = htons(atoi(argv[1]));
Are you sure there are no firewall rules causing troubles for you? Ensure that.
If the connect fails you should be able to print out the error using perror or strerror:
perror("Could not connect:");
works for me
client and server are ubuntu 12.04
for server, run in a shell
nc -l 9999
This is on a host with the address "192.168.56.13"
for client, compile code above with "DieWithError" fixed up
void DieWithError(char *errorMessage) { printf("%s",errorMessage); exit(1); }
cc -o foo foo.c
./foo 192.168.56.13 "hello" 9999</strike>
replace the DieWithError() with perror() Then I would guess that it will print out "connection refused" as you seem to have a networking problem with getting the server running on the correct address.
However, if the address in your client is correct the nc program WILL print "hello"
you just altered your program the previous version worked for me. The current version, I don't know if it does.
Like everyone else is saying, use perror() to get proper diagnostics

Server not able to receive message from client

I am using Ubuntu 12.05.I have been trying to implement Stop and wait protocol through C socket programming.I have created two programs,one featuring the server and other one the client. Expected working of the code is explained through comments
serversocket.c
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <error.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
int main(int argc,char *argv[])
{
int listenfd = 0,qlength=10,connfd = 0,t=5;
int n;
struct sockaddr_in serv;
struct sockaddr_in dest;
socklen_t socksize = sizeof(struct sockaddr_in);
char sendBuff[1024]="hi\n";
//time_t ticks;
listenfd = socket(AF_INET,SOCK_STREAM,0); //socket for listening to connection requests
memset(&serv,'0',sizeof(serv));
serv.sin_family = AF_INET;
serv.sin_addr.s_addr = htonl(INADDR_ANY);
serv.sin_port=htons(5000);
bind(listenfd,(struct sockaddr *)&serv,sizeof(serv)); //binding the socket to a port
listen(listenfd,2);
connfd=accept(listenfd,(struct sockaddr *)&dest,&socksize); //another socket for sending and receiving on the previously built socket
while(connfd){
printf("Incoming connection from %s-sending a hi...\n",inet_ntoa(dest.sin_addr));
send(connfd,sendBuff,strlen(sendBuff),0); //at first my server sends a hi
sleep(3);
n=recv(connfd,sendBuff,1024,0); //if hi received by the client,then server should receive "Message Received" from the client
sendBuff[n]='\0';
printf("%s",sendBuff);
connfd=accept(listenfd,(struct sockaddr *)&dest,&socksize); }
return 0;
}
clientsocket.c
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
int sockfd = 0, n = 0,m=2;
char recvBuff[1024];
struct sockaddr_in serv_addr;
if(argc != 2)
{
printf("\n Usage: %s <ip of server> \n",argv[0]);
return 1;
}
memset(recvBuff, '0',sizeof(recvBuff));
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Error : Could not create socket \n");
return 1;
}
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(5000);
if(inet_pton(AF_INET, argv[1], &serv_addr.sin_addr)<=0)
{
printf("\n inet_pton error occured\n");
return 1;
}
if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\n Error : Connect Failed \n");
return 1;
}
while(m--){ //m is 2 initially,I want that sending and receiving should be done 2 times
n = recv(sockfd,recvBuff,1024,0);
recvBuff[n]='\0'; //"hi" is received
printf("%s",recvBuff);
if(recvBuff=="hi\n")
send(sockfd,"Message Received",strlen("Message Received\n"),0);} //sending "Message received"
return 0;
}
Now sending messages from server to client works fine, but client to server(Message received) is creating problems.Neither it is giving an error, nor correct results,it just gives a blank screen.
You cannot compare strings using the "==" operator.
You need to use strcmp()
So....
if( strcmp( recvBuff, "hi\n" ) == 0 )
send(sockfd,"Message Received",strlen("Message Received\n"),0);
else
printf( "[%s] != [hi\n]\n", recvBuff );
Basically, you have three different issue in your code:
the string comparison (fixed by K Scott Piel).
The print of the received message are not coming out. You need to add '\n' in all your printf functions.
This is the most important issue: after the first communication between the server and the client, you are calling the accept function in the server code. From the man accept page, we can read
The accept() function shall extract the first connection on the
queue of pending connections, create a new socket with the same socket
type protocol and address family as the specified socket, and allocate
a new file descriptor for that socket.
So, after the first communication, your server is waiting for a new connection and your client is waiting for a message from the server. The second communication will never happen.
To solve this issue, you can use fork() API.
Here is a fix proposal using the fork():
clientsocket.c
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
int sockfd = 0, n = 0,m=2;
char recvBuff[1024];
struct sockaddr_in serv_addr;
if(argc != 2)
{
printf("\n Usage: %s <ip of server> \n",argv[0]);
return 1;
}
memset(recvBuff, '0',sizeof(recvBuff));
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Error : Could not create socket \n");
return 1;
}
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(5000);
if(inet_pton(AF_INET, argv[1], &serv_addr.sin_addr)<=0)
{
printf("\n inet_pton error occured\n");
return 1;
}
if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\n Error : Connect Failed \n");
return 1;
}
//m is 2 initially,I want that sending and receiving should be done 2 times
while(m--)
{
printf("Waiting from the server ...\n");
n = recv(sockfd,recvBuff,1024,0);
recvBuff[n]= '\0'; //"hi" is received
printf("%s\n", recvBuff);
if(!strncmp(recvBuff, "hi\n", strlen("hi\n")))
{
printf("Send ACK\n");
n = send(sockfd,"Message Received",strlen("Message Received\n"),0);
printf("%d Sent ... \n", n);
}
} //sending "Message received"
return 0;
}
serversocket.c
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <error.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
int main(int argc,char *argv[])
{
int listenfd = 0,qlength=10,connfd = 0,t=5;
int n;
struct sockaddr_in serv;
struct sockaddr_in dest;
socklen_t socksize = sizeof(struct sockaddr_in);
char sendBuff[1024]="hi\n";
pid_t pid;
int m = 2;
//time_t ticks;
listenfd = socket(AF_INET,SOCK_STREAM,0); //socket for listening to connection requests
memset(&serv,'0',sizeof(serv));
serv.sin_family = AF_INET;
serv.sin_addr.s_addr = htonl(INADDR_ANY);
serv.sin_port=htons(5000);
bind(listenfd,(struct sockaddr *)&serv,sizeof(serv)); //binding the socket to a port
printf("Server started ...\n");
listen(listenfd,2);
connfd=accept(listenfd,(struct sockaddr *)&dest,&socksize); //another socket for sending and receiving on the previously built socket
while(connfd > 0)
{
printf("Incoming connection from %s-sending a hi...\n",inet_ntoa(dest.sin_addr));
pid = fork();
if(pid) {
/* shall continue to listen to new client */
printf("Parent shall continue listening ... \n");
connfd=accept(listenfd,(struct sockaddr *)&dest,&socksize);
} else {
printf("Chlid process: Communication with the client ... \n");
while(m--)
{
printf("try %d - Sending %s ... \n", m, sendBuff);
send(connfd,sendBuff,strlen(sendBuff),0); //at first my server sends a hi
sleep(3);
n=recv(connfd, sendBuff, 1024, 0); //if hi received by the client,then server should receive "Message Received" from the client
sendBuff[n]='\0';
printf("Data received: [%s]\n", sendBuff); /* need to add '\n' so the print will be displayed */
strcpy(sendBuff, "hi\n");
}
printf("Child process will exit\n");
return 0;
}
}
return 0;
}

C / Linux - How to connect and read the answer of a mail server like gmail or outlook with sockets?

I would like to connect to a mail server to read the mail. For example, I have an outlook address toto#outlook.com, and I want to get all the mail of this mailbox and be able to read it. So I believe I only have to do a client, but : How do I contact and connect to this type of servers, and how do I listen the answer?
Here is my current client code :
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
int main()
{
struct protoent *pe;
int fd_client;
struct sockaddr_in s_in;
int port;
char *ip;
char buff[4096];
ip = strdup("157.56.242.251"); // I get it while calling "ping outlook.com"
port = 210;
pe = getprotobyname("TCP");
fd_client = socket(AF_INET, SOCK_STREAM, pe->p_proto);
s_in.sin_family = AF_INET;
s_in.sin_port = htons(port);
s_in.sin_addr.s_addr = inet_addr(ip);
if (connect(fd_client, (struct sockaddr *)&s_in, sizeof(s_in)) == -1)
{
if (close(fd_client) == -1)
return (-1);
fprintf(stderr, "Failed to connect\n");
return (-1);
}
//From here I don't know what to do, so I tried this
recv(fd_client, buff, 4096, 0);
printf("%s\n",buff);
write(fd_client, "Hello !\n", strlen("Hello !\n"));
//To Here
if (close(fd_client) == -1)
return (-1);
return (0);
}
Thanks in advance.

Resources