Sponsored Content
Top Forums Programming To write C Script for connecting to a Server Post 302300774 by Nagesh on Wednesday 25th of March 2009 02:57:01 AM
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);
}
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
GETGRENT_R(3)						     Linux Programmer's Manual						     GETGRENT_R(3)

NAME
getgrent_r, fgetgrent_r - get group file entry reentrantly SYNOPSIS
#include <grp.h> int getgrent_r(struct group *gbuf, char *buf, size_t buflen, struct group **gbufp); int fgetgrent_r(FILE *fp, struct group *gbuf, char *buf, size_t buflen, struct group **gbufp); Feature Test Macro Requirements for glibc (see feature_test_macros(7)): getgrent_r(): _GNU_SOURCE fgetgrent_r(): _SVID_SOURCE DESCRIPTION
The functions getgrent_r() and fgetgrent_r() are the reentrant versions of getgrent(3) and fgetgrent(3). The former reads the next group entry from the stream initialized by setgrent(3). The latter reads the next group entry from the stream fp. The group structure is defined in <grp.h> as follows: struct group { char *gr_name; /* group name */ char *gr_passwd; /* group password */ gid_t gr_gid; /* group ID */ char **gr_mem; /* group members */ }; The nonreentrant functions return a pointer to static storage, where this static storage contains further pointers to group name, password and members. The reentrant functions described here return all of that in caller-provided buffers. First of all there is the buffer gbuf that can hold a struct group. And next the buffer buf of size buflen that can hold additional strings. The result of these functions, the struct group read from the stream, is stored in the provided buffer *gbuf, and a pointer to this struct group is returned in *gbufp. RETURN VALUE
On success, these functions return 0 and *gbufp is a pointer to the struct group. On error, these functions return an error value and *gbufp is NULL. ERRORS
ENOENT No more entries. ERANGE Insufficient buffer space supplied. Try again with larger buffer. CONFORMING TO
These functions are GNU extensions, done in a style resembling the POSIX version of functions like getpwnam_r(3). Other systems use proto- type struct group *getgrent_r(struct group *grp, char *buf, int buflen); or, better, int getgrent_r(struct group *grp, char *buf, int buflen, FILE **gr_fp); NOTES
The function getgrent_r() is not really reentrant since it shares the reading position in the stream with all other threads. EXAMPLE
#define _GNU_SOURCE #include <grp.h> #include <stdio.h> #include <stdlib.h> #define BUFLEN 4096 int main(void) { struct group grp, *grpp; char buf[BUFLEN]; int i; setgrent(); while (1) { i = getgrent_r(&grp, buf, BUFLEN, &grpp); if (i) break; printf("%s (%d):", grpp->gr_name, grpp->gr_gid); for (i = 0; ; i++) { if (grpp->gr_mem[i] == NULL) break; printf(" %s", grpp->gr_mem[i]); } printf(" "); } endgrent(); exit(EXIT_SUCCESS); } SEE ALSO
fgetgrent(3), getgrent(3), getgrgid(3), getgrnam(3), putgrent(3), group(5) COLOPHON
This page is part of release 3.25 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. GNU
2007-07-26 GETGRENT_R(3)
All times are GMT -4. The time now is 02:14 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy