To write C Script for connecting to a Server


 
Thread Tools Search this Thread
Top Forums Programming To write C Script for connecting to a Server
# 1  
Old 03-25-2009
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.
# 2  
Old 03-25-2009
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

Last edited by gautamdheeraj; 03-25-2009 at 02:37 AM..
# 3  
Old 03-25-2009
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
# 4  
Old 03-25-2009
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);
}
# 5  
Old 03-25-2009
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.
# 6  
Old 03-25-2009
Quote:
Originally Posted by pludi
Shell scripts, OTOH, are interpreted by the shell, as a quick way to automate routine tasks.
.
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 ?
# 7  
Old 03-25-2009
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:
Quote:
C is a general-purpose computer programming language originally developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories to implement the Unix operating system.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

How to write if condition in shell script for application server?

Hi all, I have a code to create folder in application server through shell script and i want to create if conditional based folder folder=$HOME/test/sample/whatever if ; then echo "$folder already exists, not created." else mkdir -p "$folder" > /dev/null 2>&1 ... (7 Replies)
Discussion started by: Boost
7 Replies

2. Shell Programming and Scripting

Connecting to Windows server from UNIX through script

I am trying to connect to a Windows server say 10.1.1.10. This servers has a folder named RAJ in which there are multiple .zip files. All these zip files contain a text file called XYZ.txt. Now i have to merge the content of these XYZ.txt files from each of the .zip file and create a new text... (1 Reply)
Discussion started by: swapniljadav
1 Replies

3. Shell Programming and Scripting

Script connecting to SFTP server

hi, i have to type a script that connect to a server SFTP(password not required) and run some easy command (e.g. ls,rm,mv,etc...). I wrote into the script the connection-string and the other commands. When I run it, it connects perfectly to the server but it stops at the home. Here is... (8 Replies)
Discussion started by: zangarules
8 Replies

4. Shell Programming and Scripting

How to write a shell script for rsync to remote server?

Hello, Can you help me to write a shell script for rsync a folder from my server to remote server ? i do this in ssh by this command : rsync -avz -e ssh /copy/me/ login@serverip:/where/to i want have a shell script that do this command. and you know that this command ask remote... (0 Replies)
Discussion started by: Desperados
0 Replies

5. Programming

How to write a shell script to connect to another server?

Hello friends I want to write a script in which I will connect to my friends network. I want to use SSH. Even they can use the script to log into my network and copy files. ssh user@hostname command I know the following command will help me to log into Google's servers and see all the... (12 Replies)
Discussion started by: Angelo
12 Replies

6. Shell Programming and Scripting

Connecting to multiple unix server from unix server using shell script

Hi Gurus, I'm a unix newbie and I would like to connect to multiple unix servers from unix server using shell script i.e from server a to server b,c,d etc. I want to copy the files from unix server a to server b, c, d. I can access staright using ssh without the need to have password and user... (5 Replies)
Discussion started by: sexyTrojan
5 Replies

7. Shell Programming and Scripting

Connecting other server using ssh!

Hi All, Here i am having a problem in my script....:) i have one script which will connect from my linux box to antoher linux box.. let say...currently i am in 55.23.621.123 and i want to connect to another box which is 55.23.621.118 as we know we can connect using ssh... (2 Replies)
Discussion started by: Shahul
2 Replies

8. Solaris

NFS write failed for server.....error 11 (RPC: Server can't decode arguments)

Hello! I have a Linux nfs server (called server100 below) with a export nfs. My problem is that the Solaris client (called client100 below) doesn't seems to like it. In the Solaris syslog I got following messages (and after a while the solaris client behave liked its hanged/to buzy). Also see... (3 Replies)
Discussion started by: sap4ever
3 Replies

9. Shell Programming and Scripting

How to write a script by using unix commands to down any server

Hi, I need to do frequently server down and up. Every time i am using nearly 5 to 6 commands to down and agin i am using the commands to up. Can anybody help me to write a script to down and up. which i can use once on unix platform it can down later it can up the server. (1 Reply)
Discussion started by: sreerao
1 Replies

10. UNIX for Dummies Questions & Answers

Connecting to SUN server from PC

I have old SUN server on my desk. I know it worked before. I want to be able to connect to server from my PC. Just to establish a dump terminal. 1. I connected to power socket 2. I Have a cable that fits in (i believe is a com ports there are A B slots the same I plugged it in A). They are... (33 Replies)
Discussion started by: zam
33 Replies
Login or Register to Ask a Question