To write C Script for connecting to a Server

Hi,

I am little bit new to this scripting langauge as such, though I know the basics.
I want to write a C script which connect to a server which is listening on tcp/ip via port number 6901 with username and password and then i have to call various services to test the server.
Can some one help me write a script to connect to the server using tcp/ip using C?

thanks in advance,
Nagesh.

Using C, you will be writing programs not scripts.
Anyway what help do you need? I won't mind in helping you. Go ahead and post your questions/queries? I believe this should have been posted to other forum

-Dheeraj

thanks gowtham but my manager told we can write scripts using !! but it seems like she would have meant writing shell scripts...

anyways can you tell be how to write a bash shell script which works on linux machine version - GNU/Linux 2.6.9 - which connects to a server with tcp/ip using username and password and at port number 6901. thanks

BELOW IS THE C CODE FOR CONNECTING TO A SERVER ON CERTAIN PORT. Can someone help me to convert this in to a script format ( shell script that runs with bash - linux GNU/2.6.9 machine ) thanks...

#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <linux/in.h>
#include <sys/socket.h>
#include <unistd.h>

#define buflen 512
unsigned int portno = 3333;
char hostname[] = "192.168.100.2";

char buf[buflen]; / declare global to avoid stack */

void dia(char *sz) { printf("Dia %s\n", sz); }

int printFromSocket(int sd, char buf)
{
int len = buflen+1;
int continueflag=1;
while((len >= buflen)&&(continueflag)) /
quit b4 U read an empty socket /
{
len = read(sd, buf, buflen);
write(1,buf,len);
buf[buflen-1]='\0'; /
Note bug if "Finished" ends the buffer /
continueflag=(strstr(buf, "Finished")==NULL); /
terminate if server says "Finished" */
}
return(continueflag);
}

main()
{
int sd = socket(AF_INET, SOCK_STREAM, 0); /* init socket descriptor */
struct sockaddr_in sin;
struct hostent *host = gethostbyname(hostname);
char buf[buflen];
int len;

/*** PLACE DATA IN sockaddr_in struct ***/
memcpy(&sin.sin_addr.s_addr, host->h_addr, host->h_length);
sin.sin_family = AF_INET;
sin.sin_port = htons(portno);

/*** CONNECT SOCKET TO THE SERVICE DESCRIBED BY sockaddr_in struct ***/
if (connect(sd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
{
perror("connecting");
exit(1);
}

sleep(1); /* give server time to reply /
while(1)
{
printf("\n\n");
if(!printFromSocket(sd, buf)) break;
fgets(buf, buflen, stdin); /
remember, fgets appends the newline /
write(sd, buf, strlen(buf));
sleep(1); /
give server time to reply */
}
close(sd);
}

Short answer: (most probably) no one
Long answer: C programs are converted to machine code. There are no luxuries, you have to know what you want to do, because you'll, for the most part, have to do it yourself. Shell scripts, OTOH, are interpreted by the shell, as a quick way to automate routine tasks.

If you want to do socket programming, do it in C. If you want to communicate with a service, use a tool like telnet or netcat. If you want to communicate with a service which uses a binary protocol (as opposed to HTTP/SMTP/FTP/...), use C.

I did not understand the full form of OTOH?

thanks a lot for your elaborate reply .. now I got a better idea as to why scripts are used and why code is used.. !!
so I will try to code it in c and come up with a .EXE.

Is there anyway to call a .EXE( a application written in C ) in a script ?

OTOH - On The Other Hand / as opposed to / the other side of the matter

You call your executables just like any other UNIX program. grep, sed, awk, ... are all compiled programs, mostly written in C. Quote from Wikipedia on C:

You may also use other scripting language e.g. perl/python for your work.