0

I'm totally new to socket programming so kindly please bear with me.I'll trying to connect to tcp server which is running in cloud9 from local machine.Is it possible to connect?,if yes how?

Server.c

#include<stdio.h>
#include<string.h>    //strlen
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
#include<unistd.h>
int main(int argc , char *argv[])
{
    int socket_desc , client_sock , c , read_size;
struct sockaddr_in server , client;
char client_message[2000];

//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
    printf("Could not create socket");
}
puts("Socket created");

//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr("0.0.0.0");
server.sin_port = htons( 8080 );

//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
    //print the error message
    perror("bind failed. Error");
    return 1;
}
puts("bind done");

//Listen
listen(socket_desc , 3);

//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);

//accept connection from an incoming client
client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
if (client_sock < 0)
{
    perror("accept failed");
    return 1;
}
puts("Connection accepted");

//Receive a message from client
while( (read_size = recv(client_sock , client_message , 2000 , 0)) > 0 )
{
    //Send the message back to client
    write(client_sock , client_message , strlen(client_message));
}

if(read_size == 0)
{
    puts("Client disconnected");
    fflush(stdout);
}
else if(read_size == -1)
{
    perror("recv failed");
}

return 0;
}  

client.c

#include <stdio.h>
#include <string.h> 
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <strings.h>
int main()
{
 int sock,i;
 struct sockaddr_in server;
 char message[100] , server_reply[100];
 struct hostent *lh = gethostbyname("tcpserver- chaturasan.c9users.io");
  *message = '\0';
  *server_reply = '\0';
  printf("%s %s",message,server_reply);
 //Create socket
 sock = socket(AF_INET , SOCK_STREAM , 0);
 if (sock == -1)
 {
    printf("Could not create socket");
 }
 puts("Socket created");
 server.sin_addr.s_addr = inet_addr("104.155.229.222");
 //bcopy ( lh->h_addr, &(server.sin_addr.s_addr), lh->h_length);
 server.sin_family = AF_INET;
 server.sin_port = htons(8080);

//Connect to remote server
if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
{
    perror("connect failed. Error");
    return 1;
}

puts("Connected\n");

//keep communicating with server
while(1)
{
    printf("Enter message : ");
    message[100]='\0';
    scanf("%s" , message);

    //Send some data
    if( send(sock , message , strlen(message) , 0) < 0)
    {
        puts("Send failed");
        return 1;
    }

    //Receive a reply from the server
    if( recv(sock , server_reply , 2000 , 0) < 0)
    {
        puts("recv failed");
        break;
    }

    puts("Server reply :");
    printf("%s",server_reply);
    server_reply[100]='\0';
}

close(sock);
return 0;
}

The server output is like this

Socket created
bind done
Waiting for incoming connections...

Client output is

 Socket created
 Connected
 Enter message : 

It shows client is connected but server shows no connections

Chaturasan
  • 23
  • 5
  • Was there an error message? And if so, (a) what? and (b) why was it omitted from the question? – user207421 Jul 14 '16 at 09:57
  • output of client program is like this Socket created Connected Enter message : server ouput: socket created bind done Waiting for incoming connections...... it shows client connected but server shows no connections – Chaturasan Jul 14 '16 at 10:05
  • If you type something in the client does it get printed back? – user253751 Jul 14 '16 at 10:17
  • no it stucks there – Chaturasan Jul 14 '16 at 10:22
  • You appear to have connected to something else. Hard to believe in `tcpserver- chaturasan.c9users.io` as a valid hostname. – user207421 Jul 14 '16 at 10:34
  • The app running in cloud9 is shown in this url https://tcpserver- chaturasan.c9users.io so i tried to connect to that by getting ip of it – Chaturasan Jul 14 '16 at 10:45
  • Try using *netcat* or *socat* tool to test both your server and your client (locally). That is, use the tool to act as server and as client, and use your own client and server with it respectively, and see how it goes. Google for howtos (*socat* is more complex but also more versatile, and note that there are two different versions of *nc* with slightly different command line options), and remember to add '-v' command line options for the tool. – hyde Jul 14 '16 at 12:00
  • @Chaturasan try a different port than 8080, 8081, or 8082. Try something like 9421. I was able to connect to 8080, 8081, and 8082 so there is some sort of service using these ports already. – Brandon Jul 14 '16 at 12:04
  • @Brandon it doesn't even show connected with other ports – Chaturasan Jul 14 '16 at 14:59
  • Is it possible to connect to non web server running in cloud9 from external client? – Chaturasan Jul 14 '16 at 15:00
  • @Chaturasan I think at this point you should be talking to someone at cloud9 support. They can tell you if this is possible and exactly what IP address and port to use. – Brandon Jul 15 '16 at 00:04

1 Answers1

1

Telnet your server at 104.155.229.222, port 8080. If you can connect, then your server should output "Connection accepted" (not at your telnet prompt but wherever your server messages happen to show up). At least, you will know that your server is listening at the address/port you intend. If you can conenct but don't see the "Connection accepted" message, then there is the possibility that you are connecting to some other server.

clarasoft-it
  • 201
  • 1
  • 5
  • Thank you for the answer, it seems that I'm connected to some other server.How can I connect to my server? – Chaturasan Jul 14 '16 at 15:13
  • This is more difficult to answer; your port numbers seem to be right (they are the same on both client and server). I would say to first check your IP address to insure that you are reaching the right host. – clarasoft-it Jul 14 '16 at 15:41
  • 1
    Sorry for the late reply, non http servers cannot be accessed using tcpserver- chaturasan.c9users.io in cloud9 – Chaturasan Jul 19 '16 at 07:09