Sponsored Content
Top Forums Programming How to program a telnet client? Post 13365 by thinker130 on Wednesday 16th of January 2002 03:43:26 AM
Old 01-16-2002
the source files(the third part)

I have forgotten to include some functions needed in file tel_cli.c, please append the belowed source code to the end of file tel_cli.c.


/*connectsock - allocate & connect a socket using TCP or TCP*/
int connectsock(const char *host,const char *service, const char *transport)
/* arguments:
* host --- name of host to which connection is desired
* service -- service associated with the desired port
* transport --- name of transport protocol to use ("tcp" or "udp")
*/
{
struct hostent *phe; /* pointer to host information entry */
struct servent *pse; /* pointer to service information entry */
struct protoent *ppe; /* pointer to protocol information entry */
struct sockaddr_in sin; /* an Internet endpoint addredd */
int s,type; /* socket descriptor and socket type */

memset(&sin,0,sizeof(sin));
sin.sin_family=AF_INET;

/* Map service name to port number */
if (pse = getservbyname(service,transport))
sin.sin_port = pse->s_port;
else if ((sin.sin_port = htons((u_short)atoi(service))) == 0 )
{
//errexit("can't get \"%s\" service entry\n",service);
printf("can't get \"%s\" service entry\n",service);
return 0;
}

/* Map host name to IP address,allowing for dotted decimal */
if (phe = gethostbyname(host))
memcpy(&sin.sin_addr,phe->h_addr,phe->h_length);
else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE)
{
//errexit("can't get \"%s\" protocol entry\n",transport);
printf("can't get \"%s\" protocol entry\n",transport);
return 0;
}

/* Map transport protocol name to protocol number */
if ((ppe = getprotobyname(transport))==0)
{
//errexit("can't get \"%s\" protocol entry\n",transport);
printf("can't get \"%s\" protocol entry\n",transport);
return 0;
}

/* User protocol to choose a socket type */
if (strcmp(transport,"udp") == 0)
type = SOCK_DGRAM;
else
type = SOCK_STREAM;

/*Allocate a socket */
s = socket(PF_INET, type, ppe->p_proto);
if (s < 0)
{
//errexit("can't create socket:%s\n", strerror(errno));
printf("can't create socket:%s\n", strerror(errno));
return 0;
}
/* connect the socket */
if (connect (s,(struct sockaddr *)&sin,sizeof(sin)) < 0)
{
//errexit("can't connect to %s.%s: %s\n",host,service,strerror(errno));
printf("can't connect to %s.%s: %s\n",host,service,strerror(errno));
return 0;
}
return s;

}


int connectTCP(const char *host, const char *service)
/* arguments:
* host --- name of host to which connection is desired
* service --- service associated with the desired port
*/
{
return connectsock(host,service,"tcp");
}




/* errexit --- print an error mesage and exit */

/*VARARGS1*/

int errexit(const char *format,...)
{
va_list args;
va_start(args,format);
vfprintf(stderr,format,args);
va_end(args);
exit(1);
}
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Using telnet client from MacOSX's command line terminal

I'm completely new to Unix, but familiar with Mac OSX. I've just discovered the command line terminal feature of this new OS and I'm trying to learn how to telnet into my host's server to change permissions to allow executable cgi scripts for my website. Is there anyone who might be able to... (2 Replies)
Discussion started by: tylerl
2 Replies

2. Programming

Telnet client IP determination

I have configured my firewall to allow only five remote IP's to connect to my server. Upon connection...i would like to automate the Xsession functions for authorized IP's. Mainly, $DISPLAY of the environment to the client. I understand that the "gethostbyaddr" function is capable of this.... (0 Replies)
Discussion started by: thomas.jones
0 Replies

3. Programming

Does Telnet client library exist ?

Hello, I'm new user in this forum, and i'm french ... so excuse me for my english :) In fact, I've to manage several network equipments which can be only configured with the telnet protocol. But using the standalone telnet tool, it's too long and hard. So I'd like to known if a Telnet... (0 Replies)
Discussion started by: aho
0 Replies

4. UNIX for Dummies Questions & Answers

IP address of telnet client

Hi, I am using the telnet client on windowsNT to access the Unix system. I want to find out the IP address of the telnet client from the server side. The reason is I want to set the DISPLAY environment variable when the user is logged on. Is there anyway that the the Unix server can... (1 Reply)
Discussion started by: vtran4270
1 Replies

5. UNIX for Dummies Questions & Answers

how to launch program though telnet

I have launched telnet on nt and have connected to a unix server, I'm trying to run a program on the unix server which will launch a gui, but when I try to launch it I get the message "display not set\n" "By default set it to 0.0" I can sit down at the unix machine and launch the program with... (3 Replies)
Discussion started by: cbachman
3 Replies

6. Programming

Client - server program

i came acors this coding when surfin the net.this code works perfectly.but as i am new to this socket programming i need sm coments quoted on it or explanation regarding this source code. i have prb understanding the server.c i have posted it below can u guys help me !!!! cheerZ The... (4 Replies)
Discussion started by: mathu
4 Replies

7. Programming

Server client program

hi guys, I need the code for a server client registration form.The server must ask for authentication .Then the client would send in data. This is stored in a file .The server sends back a receipt to the client as part of the payment done. plz can some 1 get me the code... (9 Replies)
Discussion started by: pip3r
9 Replies

8. Programming

SFTP client program

can u help me? i need the program code in C to perform Simple File Transfer in linux.in this forum i found the server program,inw i need the client program ASAP. Thanx. (1 Reply)
Discussion started by: harshi
1 Replies

9. UNIX for Advanced & Expert Users

difference between logging into unix through f-secure ssh client and telnet

hi, what is the difference between logging into unix through f-secure ssh client and telnet is there any more security check is involved can any one explain thanks in advance (1 Reply)
Discussion started by: trichyselva
1 Replies

10. Linux

telnet client and logs

Hello everyone. I have a script that telnets into a linux workstation and restarts a service, while logging the session to a file. I'm kind of new to linux so when I was using windows I would capture something like windows cli: telnet xxx.xxx.xxx.xxx -f c:/capture.log ... # /sbin/service... (3 Replies)
Discussion started by: Elyas_cr
3 Replies
LWRES_GETADDRINFO(3)						       BIND9						      LWRES_GETADDRINFO(3)

NAME
lwres_getaddrinfo, lwres_freeaddrinfo - socket address structure to host and service name SYNOPSIS
#include <lwres/netdb.h> int lwres_getaddrinfo(const char *hostname, const char *servname, const struct addrinfo *hints, struct addrinfo **res); void lwres_freeaddrinfo(struct addrinfo *ai); If the operating system does not provide a struct addrinfo, the following structure is used: struct addrinfo { int ai_flags; /* AI_PASSIVE, AI_CANONNAME */ int ai_family; /* PF_xxx */ int ai_socktype; /* SOCK_xxx */ int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */ size_t ai_addrlen; /* length of ai_addr */ char *ai_canonname; /* canonical name for hostname */ struct sockaddr *ai_addr; /* binary address */ struct addrinfo *ai_next; /* next structure in linked list */ }; DESCRIPTION
lwres_getaddrinfo() is used to get a list of IP addresses and port numbers for host hostname and service servname. The function is the lightweight resolver's implementation of getaddrinfo() as defined in RFC2133. hostname and servname are pointers to null-terminated strings or NULL. hostname is either a host name or a numeric host address string: a dotted decimal IPv4 address or an IPv6 address. servname is either a decimal port number or a service name as listed in /etc/services. hints is an optional pointer to a struct addrinfo. This structure can be used to provide hints concerning the type of socket that the caller supports or wishes to use. The caller can supply the following structure elements in *hints: ai_family The protocol family that should be used. When ai_family is set to PF_UNSPEC, it means the caller will accept any protocol family supported by the operating system. ai_socktype denotes the type of socket -- SOCK_STREAM, SOCK_DGRAM or SOCK_RAW -- that is wanted. When ai_socktype is zero the caller will accept any socket type. ai_protocol indicates which transport protocol is wanted: IPPROTO_UDP or IPPROTO_TCP. If ai_protocol is zero the caller will accept any protocol. ai_flags Flag bits. If the AI_CANONNAME bit is set, a successful call to lwres_getaddrinfo() will return a null-terminated string containing the canonical name of the specified hostname in ai_canonname of the first addrinfo structure returned. Setting the AI_PASSIVE bit indicates that the returned socket address structure is intended for used in a call to bind(2). In this case, if the hostname argument is a NULL pointer, then the IP address portion of the socket address structure will be set to INADDR_ANY for an IPv4 address or IN6ADDR_ANY_INIT for an IPv6 address. When ai_flags does not set the AI_PASSIVE bit, the returned socket address structure will be ready for use in a call to connect(2) for a connection-oriented protocol or connect(2), sendto(2), or sendmsg(2) if a connectionless protocol was chosen. The IP address portion of the socket address structure will be set to the loopback address if hostname is a NULL pointer and AI_PASSIVE is not set in ai_flags. If ai_flags is set to AI_NUMERICHOST it indicates that hostname should be treated as a numeric string defining an IPv4 or IPv6 address and no name resolution should be attempted. All other elements of the struct addrinfo passed via hints must be zero. A hints of NULL is treated as if the caller provided a struct addrinfo initialized to zero with ai_familyset to PF_UNSPEC. After a successful call to lwres_getaddrinfo(), *res is a pointer to a linked list of one or more addrinfo structures. Each struct addrinfo in this list cn be processed by following the ai_next pointer, until a NULL pointer is encountered. The three members ai_family, ai_socktype, and ai_protocol in each returned addrinfo structure contain the corresponding arguments for a call to socket(2). For each addrinfo structure in the list, the ai_addr member points to a filled-in socket address structure of length ai_addrlen. All of the information returned by lwres_getaddrinfo() is dynamically allocated: the addrinfo structures, and the socket address structures and canonical host name strings pointed to by the addrinfostructures. Memory allocated for the dynamically allocated structures created by a successful call to lwres_getaddrinfo() is released by lwres_freeaddrinfo(). ai is a pointer to a struct addrinfo created by a call to lwres_getaddrinfo(). RETURN VALUES
lwres_getaddrinfo() returns zero on success or one of the error codes listed in gai_strerror(3) if an error occurs. If both hostname and servname are NULL lwres_getaddrinfo() returns EAI_NONAME. SEE ALSO
lwres(3), lwres_getaddrinfo(3), lwres_freeaddrinfo(3), lwres_gai_strerror(3), RFC2133(), getservbyname(3), bind(2), connect(2), sendto(2), sendmsg(2), socket(2). COPYRIGHT
Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC") Copyright (C) 2000, 2001, 2003 Internet Software Consortium. BIND9 Jun 30, 2000 LWRES_GETADDRINFO(3)
All times are GMT -4. The time now is 08:46 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy