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:
Code:
/*************************************************************************/
/* 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:
Code:
#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