----- Original Message ----- From: "Paul Patrick C. Prantilla" <[EMAIL PROTECTED]> To: "Philippine Linux Users' Group (PLUG) Technical Discussion List" <[email protected]>
Sent: Thursday, August 10, 2006 3:57 PM
Subject: Re: [plug] auto sending text to tcp port


Hello,

You can use ncat for this.

echo "ehlo mailserver.com" | nc <some ip address> 25

 <snip>
 250-PIPELINING
 250-SIZE 10240000
 250-VRFY
 250-ETRN
 250-STARTTLS
 250-AUTH LOGIN PLAIN
 250-AUTH=LOGIN PLAIN
 250 8BITMIME

Hope that helps.

-Paul

jhuniepi wrote:
yeah i always use telnet but that's my problem. i cant send string to tcp port automatically using telnet command. i have to enter the telnet environment to issue a command. And my program is the service that run on a certain tcp port. the format is just plain text with no encryption.:)

On 8/10/06, *Ariz Jacinto* <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>> wrote:

you can always use the telnet command :) sending a string is one thing
    but if you're going to send it to a known service, better comply
    with the
    format whether its XML or not.

here is a simple socket programming for your needs...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

#define HOST    "127.0.0.1"
#define PORT    80

void error(char *errmsg) {

       fprintf(stderr, "%s\n", errmsg);
       exit(1);
}

int main(int argc, char *argv[]) {
int sockfd, nsize;
char buffer[BUFSIZ];
struct sockaddr_in sin;

       memset(&sin, 0, sizeof(struct sockaddr_in));
       sin.sin_family = AF_INET;
       sin.sin_port = htons(PORT);
       inet_aton(HOST, &sin.sin_addr);
       switch (argc) {
               case 3 : sin.sin_port = htons(atoi(argv[2]));
               case 2 : inet_aton(argv[1], &sin.sin_addr);
       }
       if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
               error("socket() error!");
if (connect(sockfd, (struct sockaddr *) &sin, sizeof(struct sockaddr)) == -1)
               error("connect() error!");
       while ((nsize = read(0, buffer, sizeof(buffer))) > 0 )
               write(sockfd, buffer, nsize);
       close(sockfd);

       return (0);
}

save to sendtext.c
compile with gcc -O3 -Wall -o sendtext sendtext.c

command:
   sendtext [ip_address] [tcp_port_number]

if tcp_port_number was ommited, default tcp port number is 80
if ip_address was ommited, default ip address is 127.0.0.1

sample commands:
   * send the file to host 1.2.3.4 with tcp port 3128
       ./sendtext 1.2.3.4 3128 < file

   * send the contents of file to host 1.2.3.4 with tcp port 80
       cat file | ./sendtext 1.2.3.4

   * echo abc to host 127.0.0.1 with tcp port 80
       echo "abc" | ./sendtext

you can extend the code that accepts UDP port number by replacing SOCK_STREAM to SOCK_DGRAM... have fun!

fooler.
_________________________________________________
Philippine Linux Users' Group (PLUG) Mailing List
[email protected] (#PLUG @ irc.free.net.ph)
Read the Guidelines: http://linux.org.ph/lists
Searchable Archives: http://archives.free.net.ph

Reply via email to