Server/Client C program to work under Fedora Core Linux 6.
/******************************************libraries declarations******************/
#include <stdio.h>
#include <netdb.h> //for network
#include <sys/types.h> //for system calls,socket data types
#include <sys/socket.h> //structures for sockets
#include <stdlib.h>
#include <netinet/in.h> //structures for internet domain addresses
#include <string.h>
// Linux server port, Win server port and ip
#define PORT 1117
#define SERVER_PORT 5001
#define SERVER_IPv4 "172.24.1.37"
/*******************************************main***********************************/
int main(void)
{
struct sockaddr_in6 servaddr; //IPv6 structure for the server
struct sockaddr_in6 claddr; // IPv6 structure for the client
struct sockaddr_in clientaddr;//IPv4 structure for the client
//struct sockaddr_in serveraddr;//ipv4 structure for the server
int sock_ipv6_server, newsocketfd, cllen;
char mess[2048];
char comm[256], dest[2048], tmp[2048], v1[256], v2[1024];
char RecvComm[256];
int retval;
FILE *fp;
int i, j;
char buf[1024];
int sock_ipv4_client, servlen;
char mail[256];
// create connection to Win client
sock_ipv6_server = socket(AF_INET6, SOCK_STREAM, 0);
if (sock_ipv6_server == -1)
{
printf("Cannot create socket!!!");
exit(0);
}
printf("We managed to create the socket with the number: %d\n",sock_ipv6_server);
//bzero((char*)&servaddr,sizeof(servaddr));
memset((char *)&servaddr, 0, sizeof(servaddr));
memset((char *) &claddr,0,sizeof(claddr));
// vals for IPv6 address
servaddr.sin6_family = AF_INET6;
servaddr.sin6_port = htons(PORT);
servaddr.sin6_addr=in6addr_any;//accept any IP address defined on that machine
// actual connection step 1
retval = bind(sock_ipv6_server, (struct sockaddr *)&servaddr,sizeof(servaddr));
if (retval == -1)
{
printf ("ERROR on binding\n");
exit(0);
}
//step 2
retval = listen(sock_ipv6_server,1);
if(retval == -1)
{
printf("ERROR on listen()\n");
exit(0);
}
// step 3
cllen = sizeof(claddr);
newsocketfd = accept(sock_ipv6_server,(struct sockaddr *) &claddr,&cllen) ;
if (newsocketfd == -1) printf("ERROR on accept");
// read user list to file
system("who -q >users.txt");
//open file for read
fp=fopen("users.txt","r");
//count all
i=1;
do
{
//get one user
fscanf(fp,"%s ",v1);
i++;
}while(!feof(fp));
fclose(fp);
//read actual users
fp=fopen("users.txt","r");
for(j=1; j < i-2; j++)
{
fscanf(fp,"%s ",v1);
sprintf( v2, "%d.%s\r\n", j, v1);
strcat(tmp, v2);
}
// close file
fclose(fp);
//delete temp file
system("rm users.txt");
// test
printf(tmp);
//send message to win client
mess[0]='\0';
strcat(mess, "Bun venit!\r\n");
strcat(mess, tmp);
//debug
printf("================mess====================== ");
printf(mess);
//actual send
send(newsocketfd,mess,sizeof(mess),0) ;
// receive *COMMyy*email* from Win client
if (recv(newsocketfd,RecvComm,sizeof(RecvComm),0) == -1)
{
//means we have errors in receiving the command from the client
//we treat these errors
printf("Conection failed\n");
if (shutdown(newsocketfd,2)<0)
{
printf("We have error on closing the connection");
exit(EXIT_FAILURE);
}
printf("Connection shutdown\n");
if (close(newsocketfd)<0)
{
printf("Error on closing the socket\n");
exit(EXIT_FAILURE);
}
printf("Close connection\n");
exit(0);
}
// ok receive
printf("ok RecvComm\n");
//debug
printf("================received======================\n ");
printf(RecvComm);
printf("====================================== \n");
// Send command to Win server
//parse command
// write message to file recvComm.txt, to be able to exec cut
fp = fopen("recvComm.txt", "w");
fprintf( fp, "%s", RecvComm );
fclose(fp);
//get Comm value
system("cut -d* -f2 recvComm.txt > tmp.txt" );
fp = fopen("tmp.txt", "r");
fscanf( fp, "%s", comm);
fclose(fp);
system("cut -d* -f3 recvComm.txt > tmp.txt" );
fp = fopen("tmp.txt", "r");
fscanf( fp, "%s", mail);
fclose(fp);
//delete temp files
system("rm tmp.txt recvComm.txt");
sscanf( RecvComm, "*%s*%s", comm, mail);
printf("comm=%s email=%s\n",comm, mail);
if(strcmp(comm,"COMM01")==0)
{
// the right command
//connect to Win server
//0. create socket
sock_ipv4_client=socket(AF_INET,SOCK_STREAM,0);
if (sock_ipv4_client==-1)
{
printf("Cannot create socket!!!");
exit(0);
}
printf("We managed to create the socket with the number%d\n",sock_ipv4_client);
clientaddr.sin_family = AF_INET;
clientaddr.sin_port = htons(SERVER_PORT);
clientaddr.sin_addr.s_addr = inet_addr(SERVER_IPv4);
//step 1
retval=connect(sock_ipv4_client,(struct sockaddr*)&clientaddr,sizeof(clientaddr));
if (retval==-1)
{
printf("Error on connection to the server \n");
exit(0);
}
else
{
printf("All OK\n");
//send the command to the Win server
send(sock_ipv4_client,"netsh interface ip show address",30,0);
//receive result from Win server
retval=recv(sock_ipv4_client,buf,sizeof(buf),0);
if (retval==-1)
{
printf("ERoare mare: result from win server missing!!!!!\n");
exit(0);
}else
{
printf("BINe!\n");
//send result to Win client
send(newsocketfd,buf,sizeof(buf),0);
//write result to file
//open file for write
fp=fopen("result.txt","w");
fprintf(fp, "%s\n", buf);
//send mail
sprintf( tmp, "mail < result.txt %s", mail);
system( tmp );
}
}//else retval==-1
}// if COMM1
else
{
printf("Wrong command");
//send message to Win client
send(newsocketfd,"Command not implemented.", 24, 0);
}
//close all socket
close(newsocketfd);
close(sock_ipv6_server);
close(sock_ipv4_client);
}//main