problem with connecting to my own machine [Archive] - C Board

PDA

View Full Version : problem with connecting to my own machine


EvBladeRunnervE
07-19-2003, 12:30 PM
ok, i am starting network programming on linux, how can i connect to my own machine through the program? I have setup everything, but it either just sits there if i try connecting to some random address, or it just ends if i try to connect to my own machines address. also, when i try to telnet to my own machine , it says i dont have priveliges to do it. here is the code(i know there are probably bugs besides this, but one at a time):


client app so far:

/************************************************** ***********************/
/* Twisted, copyright 2003 , of */
/* Matthew Valley */
/* Lead Designer/Programmer */
/* CodeMonkeys, Copyright 2003 */
/************************************************** ***********************/


/************************************************** ***********************/
/* Includes: */
/* Here is where all of our needed header files */
/* Are included */
/************************************************** ***********************/

#include <stdio.h>
#include <iostream>
using namespace std;
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

/************************************************** ***********************/
/* Definitions */
/************************************************** ***********************/
#define SERVER_PORT 6666


/************************************************** ***********************/
/* Classes */
/************************************************** ***********************/

class PacketWrap
{
public :

char *whosaid;
int packlen;
char *packet;
};

class ClientInfo
{
private:

int sock; // this is our socket
struct sockaddr_in client_addr; // this is the address info needed for the socket to connect
hostent* he; // this containt our host info
PacketWrap Packet; // this contains our packet info

public:


void GetHostByName(char* argv); // this initiats and sets our host info
void SetupAndConnect(); // this sets up and connects the sockets
void Send(char *string); // sends a string
void ReadAndDisplay(); // reads in packets and displays them
};



/************************************************** ***********************/
/* FUNCTION: sendall */
/* PURPOSE : to keep sending a packet until it is finished */
/* sending */
/************************************************** ***********************/
int sendall(int s, char *buf, int *len)
{
int total = 0; // how many bytes we've sent
int bytesleft = *len; // how many we have left to send
int n;

while(total < *len) {
n = send(s, buf+total, bytesleft, 0);
if (n == -1) { break; }
total += n;
bytesleft -= n;
}

*len = total; // return number actually sent here

return n==-1?-1:0; // return -1 on failure, 0 on success
}
/************************************************** ***********************/
/* FUNCTION: ClientInfo::GetHostByName(char* argv) */
/* PURPOSE : to Setup our Host info by reading in the IP address */
/* of the server and planting it into our hostinfo */
/************************************************** ***********************/
void ClientInfo::GetHostByName(char* argv)
{

he = gethostbyname(argv);
}

/************************************************** ***********************/
/* FUNCTION: ClientInfo::SetupAndConnect() */
/* PURPOSE : To setup the address info for the socket, then */
/* Connect it to the Server */
/************************************************** ***********************/
void ClientInfo::SetupAndConnect()
{




client_addr.sin_family = AF_INET; // setup sockaddr family to AF_INET
client_addr.sin_port = htons(SERVER_PORT); // Setup Destination port
client_addr.sin_addr= *((struct in_addr *)he->h_addr); // setup Destination IP address
memset(&(client_addr.sin_zero),'\0',8); // set rest to zero



if((sock = socket(AF_INET,SOCK_STREAM,0)) == -1){ // setup our socket with error clause

perror("socket");
exit(1);
}


if(connect(sock,(struct sockaddr*)&client_addr,sizeof(struct sockaddr)) == -1){// connect to address

perror("connect");
exit(1);
}

}


/************************************************** ***********************/
/* FUNCTION: ClientInfo::Send(char *string) */
/* PURPOSE : encapsulates a packet, then sends it */
/************************************************** ***********************/

void ClientInfo::Send(char *string)
{

Packet.whosaid = inet_ntoa(client_addr.sin_addr);
Packet.packlen = strlen(strcat(Packet.whosaid,string)) + 4;
Packet.packet = Packet.whosaid;
Packet.packet = strcat(Packet.packet,(const char*)Packet.packlen);
Packet.packet = strcat(Packet.packet,string);

sendall(sock,Packet.packet,&Packet.packlen);
}

void ClientInfo::ReadAndDisplay()
{

}


int main(int argc, char *argv[]){

ClientInfo ci;
int choice;

if (argc != 2) {
fprintf(stderr,"usage: client hostname\n");
exit(1);
}
ci.GetHostByName(argv[1]); // setup he

printf (" \n************************8 Twisted 8*******************\n");
printf ("pick a choice:\n");
printf ("1) Connect\n");
printf ("2) Readme\n");
printf ("3) Exit\n");

cin >> choice;
switch (choice)
{

case 1:
ci.SetupAndConnect();
break;

case 2:
break;

case 3:
exit(1);

return 0;
}

return 0;
}


and here is the program that is supposed to respond so far:


#include <stdio.h>
#include <iostream>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#define BACKLOG 10
#define LIST_PORT 6666


int SetupSocket()
{

int sock;
struct sockaddr_in addr_info;
int yes = 1;
addr_info.sin_family = AF_INET; // setup sockaddr family to AF_INET
addr_info.sin_port = htons(LIST_PORT); // Setup Destination port
addr_info.sin_addr.s_addr = 0; // setup Destination IP address
memset(&(addr_info.sin_zero),'\0',8); // set rest to zero


if((sock = socket(AF_INET,SOCK_STREAM,0)) == -1){ // setup our socket with error clause

perror("socket");
exit(1);
}

if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) { // set socket so its reusable
perror("setsockopt");
exit(1);
}

if(bind(sock,(struct sockaddr*)&addr_info,sizeof(struct sockaddr))){ // bind the socket with error clause

perror("bind");
exit(1);
}


if(listen(sock,BACKLOG) == -1){// connect to address

perror("listen");
exit(1);
}

return sock;
}
int main()
{
int bytesent;
int sock,s_client;
socklen_t sinsize;
char buf[1024];
char szIntro[] ="Hello, welcome to Twisted Mongrel what do you want to do:\n";
char szIntro_choice[]="1) start a Hack \n 2)continue a Hack \n 3)exit\n";

struct sockaddr_in client_info;


sock = SetupSocket();



while(1){// maina accept loop

sinsize = sizeof(struct sockaddr_in);

s_client = accept(sock,(struct sockaddr*) &client_info ,&sinsize); // accept connect request

if((bytesent = send(s_client,szIntro,strlen(szIntro),0)) <= 2)// check if bytes are getting through
{
printf("no bytes getting through");
}

send(s_client,szIntro_choice,strlen(szIntro_choice ),0);



}
return 0;
}



thanks in advance

Hunter2
07-20-2003, 11:07 PM
either just sits there if i try connecting to some random addressThat's normal, unless the random address is actually one being used, and the other computer is on and has a server set up listening on the port your client uses.
or it just ends if i try to connect to my own machines address.So? Your program doesn't tell it to do anything once it connects, so it just connects then quits.

Another thing, I don't know if it's important in your case, but I don't like the ClientInfo::Send(char* string). In this case, the function will only work if string is a null-terminated string, and will probably die horribly if it isn't. Depending on whether you just want to send text strings etc. this might be very annoying if you keep having to add a null 0 to the end of your data segments.

*Also, please properly indent your code next time you post.

EvBladeRunnervE
07-21-2003, 04:20 PM
ok, actually, it is just refusing to connect, with the message "connection not allowed", and btw, if you look and see, the 'code' tags are there, and therefore i follow the rules.

Hunter2
07-21-2003, 05:32 PM
"connection not allowed"? Hold on, maybe I'm wrong, but I didn't see anything in there that displays error messages. Where did it come from?

Also, I wasn't referring to code tags :) I meant the indentation, which is the tabbing and stuff. It's pretty well indented at the top, but after GetHostByName(), there are huge white-space gaps and entire functions that are completely un-tabbed. Just thought I'd point this out, since it would be a lot easier to read that way.

EvBladeRunnervE
07-23-2003, 11:55 AM
if(connect(sock,(struct sockaddr*)&client_addr,sizeof(struct sockaddr)) == -1){// connect to address

perror("connect");
exit(1);
}


the perror prints the error if there is one.

and i will try to tab stuff and indent it, although it looks fine by me at 1024x768

Hunter2
07-23-2003, 01:06 PM
Oh, I see. But then, how do you know it's "connection not allowed"? How are you connecting to your own machine? Are you connecting by "localhost" (I hope that's not just a Windows-specific thing) or are you finding your machine's ip on the internet then trying to connect to that?

**EDIT**
I think your problem might be that you need to call WSAStartup() at the beginning of the program and and WSACleanup() at the end.. I don't see that in your code ;)

EvBladeRunnervE
07-23-2003, 04:11 PM
think your problem might be that you need to call WSAStartup() at the beginning of the program and and WSACleanup() at the end.. I don't see that in your code

actually, that is a windows specific command, and unnecesary under linux.

about connecting to my own machine, i have done it both by localhost and internet IP, i believe i might need to somehow integrate permissions in order to connect

Hunter2
07-23-2003, 04:41 PM
that is a windows specific commandAhhh damn... lol

Integrate permissions? lol didn't even know there was such thing... I thought you just needed to go bind(), listen() and accept() and then call connect()...

And anyways, how are you getting the specific message "connection not allowed"? I only see a set error message defined by you :confused:

EvBladeRunnervE
07-24-2003, 07:55 AM
I only see a set error message defined by you

no, its system defines based on different stuff, much like GetLastError(), except is done to only display connect's error.

Hunter2
07-24-2003, 11:45 AM
Ahh, I should read the docs more carefully :rolleyes: I looked and all I saw was "Prints a string", but I missed "followed by the last error"...

Hammer
07-24-2003, 05:16 PM
Are you still having troubles with this? If so:

Start your server, and run netstat -an to see connections and listening ports. Make sure you are listening on the correct port number and IP. You should see something like:
TCP 0.0.0.0:6666 0.0.0.0:0 LISTENING

If you see it listening, you can then simply telnet to it:
>>telnet localhost 6666
This will hopefully give up a clue as to the location of your problem (client or server), by eliminating the client from the test.

If this works, you could also try to connect your client to one of your existing open ports (like a telnet server or FTP server, or any benign service). Again, this is only for a kind of debugging exercise, helping you to pin point your problem.