#include <gtk/gtk.h>
#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>
#include <errno.h>
#define BUFSIZE 1024
GtkWidget *g_txtBlck_userName;
GtkWidget *g_txtBlck_password;
GtkWidget *g_contactsList;
GtkWidget *g_songsFlowBox;
GtkWidget *g_entry_Username;
GtkWidget *loginWindow;
GtkWidget *mainWindow;
GtkWidget *fileTextBlock;
GtkWidget *textLabel;
char userNameTxt[20];
int main(int argc, char *argv[])
{
gtk_init(&argc, &argv);
GtkBuilder *builder;
builder = gtk_builder_new();
gtk_builder_add_from_file (builder, "glade/window_main.glade", NULL);
loginWindow = GTK_WIDGET(gtk_builder_get_object(builder, "loginWindow"));
gtk_builder_connect_signals(builder, NULL);
g_txtBlck_userName = GTK_WIDGET(gtk_builder_get_object(builder, "usrTextblock"));
g_txtBlck_password = GTK_WIDGET(gtk_builder_get_object(builder, "pswrdTextblock"));
g_object_unref(builder);
gtk_widget_show(loginWindow);
gtk_main();
return 0;
}
void startSession()
{
/* Connection Establishment */
/* GUI CODE */
gtk_widget_hide(loginWindow);
GtkBuilder *builder;
builder = gtk_builder_new();
gtk_builder_add_from_file (builder, "glade/window_main.glade", NULL);
mainWindow = GTK_WIDGET(gtk_builder_get_object(builder, "mainWindow"));
g_contactsList = GTK_WIDGET(gtk_builder_get_object(builder, "contactsListBox"));
g_songsFlowBox = GTK_WIDGET(gtk_builder_get_object(builder, "songsFlowBox"));
g_entry_Username = GTK_WIDGET(gtk_builder_get_object(builder, "page3UserTxtBlck"));
fileTextBlock = GTK_WIDGET(gtk_builder_get_object(builder, "fileTxtBlock"));
textLabel = GTK_WIDGET(gtk_builder_get_object(builder, "textLabel"));
const gchar *str_pswrd;
const gchar *str_usrName;
str_usrName = gtk_entry_get_text((GtkEntry *)g_txtBlck_userName);
str_pswrd = gtk_entry_get_text((GtkEntry *)g_txtBlck_password);
int i;
for(i = 0; i < strlen(str_usrName); i++)
{
if(str_usrName[i] != ' ')
{
userNameTxt[i] = str_usrName[i];
}
else
{
break;
}
}
gtk_builder_connect_signals(builder, NULL);
gtk_entry_set_text((GtkEntry *) g_entry_Username, userNameTxt);
gtk_widget_show(mainWindow);
}
// called when window is closed
void on_window_main_destroy()
{
gtk_main_quit();
}
int isValid(char *one, char *two)
{
int i = 0;
while(i < (strlen(two) - 1))
{
if(one[i] != two[i])
{
return 0;
}
i++;
}
return 1;
}
void connect_request(int *sockfd, struct sockaddr_in *server_addr)
{
if ((*sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Socket");
exit(1);
}
server_addr->sin_family = AF_INET;
server_addr->sin_port = htons(4950);
server_addr->sin_addr.s_addr = inet_addr("127.0.0.1");
memset(server_addr->sin_zero, '\0', sizeof server_addr->sin_zero);
if(connect(*sockfd, (struct sockaddr *)server_addr, sizeof(struct sockaddr)) == -1) {
perror("connect");
exit(1);
}
}
void send_recv(int i, int sockfd)
{
char send_buf[BUFSIZE];
char recv_buf[BUFSIZE];
int nbyte_recvd;
if (i == 0)
{
fgets(send_buf, BUFSIZE, stdin);
if (strcmp(send_buf , "quit\n") == 0) {
exit(0);
}
else
send(sockfd, send_buf, strlen(send_buf), 0);
}
else
{
nbyte_recvd = recv(sockfd, recv_buf, BUFSIZE, 0);
recv_buf[nbyte_recvd] = '\0';
fflush(stdout);
}
}
void on_createAccount_clicked()
{
/*
const gchar *str_pswrd;
const gchar *str_usrName;
str_usrName = gtk_entry_get_text((GtkEntry *)g_txtBlck_userName);
str_pswrd = gtk_entry_get_text((GtkEntry *)g_txtBlck_password);
char *check = strcat((char *)str_usrName, " ");
check = strcat((char *)str_usrName, (char *)str_pswrd);
check = strcat(check, "\0");
FILE *f = fopen("resources/Contacts.txt", "a");
fseek(f, 0, SEEK_END);
if (f == NULL)
{
printf("Error opening file!\n");
exit(1);
}
fprintf(f, "%s\n", check);
*/
int sockfd, fdmax, i;
struct sockaddr_in server_addr;
fd_set master;
fd_set read_fds;
connect_request(&sockfd, &server_addr);
FD_ZERO(&master);
FD_ZERO(&read_fds);
FD_SET(0, &master);
FD_SET(sockfd, &master);
fdmax = sockfd;
int pid;
pid = fork();
if(pid != 0)
{
while(1){
read_fds = master;
if(select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1){
perror("select");
exit(4);
}
for(i=0; i <= fdmax; i++ )
if(FD_ISSET(i, &read_fds))
send_recv(i, sockfd);
}
close(sockfd);
}
else
{
char send_buf[BUFSIZE];
int recv;
int nbyte_recvd;
send(sockfd, "create", strlen("create"), 0);
const gchar *str_pswrd;
const gchar *str_usrName;
str_usrName = gtk_entry_get_text((GtkEntry *)g_txtBlck_userName);
str_pswrd = gtk_entry_get_text((GtkEntry *)g_txtBlck_password);
char *check = strcat((char *)str_usrName, " ");
check = strcat((char *)str_usrName, (char *)str_pswrd);
check = strcat(check, "\0");
send(sockfd, check, strlen(check), 0);
close(sockfd);
}
}
void on_loginButton_clicked()
{
const gchar *str_pswrd;
const gchar *str_usrName;
int sockfd, fdmax, i;
struct sockaddr_in server_addr;
fd_set master;
fd_set read_fds;
connect_request(&sockfd, &server_addr);
FD_ZERO(&master);
FD_ZERO(&read_fds);
FD_SET(0, &master);
FD_SET(sockfd, &master);
fdmax = sockfd;
int pid;
pid = fork();
if(pid != 0)
{
while(1){
read_fds = master;
if(select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1){
perror("select");
exit(4);
}
for(i=0; i <= fdmax; i++ )
if(FD_ISSET(i, &read_fds))
send_recv(i, sockfd);
}
printf("client-quited\n");
close(sockfd);
}
else
{
str_usrName = gtk_entry_get_text((GtkEntry *)g_txtBlck_userName);
str_pswrd = gtk_entry_get_text((GtkEntry *)g_txtBlck_password);
/*
FILE *f = fopen("resources/Contacts.txt", "w");
if (f == NULL)
{
printf("Error opening file!\n");
exit(1);
}
fprintf(f, "%s", str_usrName);
fprintf(f, "%s\n", str_pswrd);
*/
char *check = strcat((char *)str_usrName, " ");
check = strcat((char *)str_usrName, (char *)str_pswrd);
check = strcat(check, "\0");
send(sockfd, check, strlen(check), 0);
char recv_buf[1];
recv(sockfd, recv_buf, sizeof(recv_buf), 0);
if(atoi(recv_buf))
{
startSession();
}
close(sockfd);
}
}
void on_openButton_clicked()
{
if(gtk_entry_get_text((GtkEntry *)fileTextBlock) == ""
|| gtk_entry_get_text((GtkEntry *)fileTextBlock) == NULL)
return;
int sd;
struct sockaddr_in servaddr;
char sendline[4096], recvline[4096];
//Create a socket for the client
//If sd<0 there was an error in the creation of the socket
if ((sd = socket (AF_INET, SOCK_STREAM, 0)) <0) {
perror("Problem in creating the socket");
exit(2);
}
//Creation of the socket
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr= inet_addr("127.0.0.1");
servaddr.sin_port = htons(3000); //convert to big-endian order
//Connection of the client to the socket
if (connect(sd, (struct sockaddr *) &servaddr, sizeof(servaddr))<0) {
perror("Problem in connecting to the server");
exit(3);
}
else
printf("\nconnected to server now.\n");
/*file start*/
int n;
char buf_recv[1000];
char *buf_send;
char *filename;
char file_buffer[10000];
FILE *fp;
//code for receivind statement "Enter the file name"
n = recv(sd, buf_recv, sizeof(buf_recv), 0);
buf_recv[n]='\0';
printf("%s",buf_recv);
buf_send = gtk_entry_get_text((GtkEntry *)fileTextBlock);
send(sd,buf_send,strlen(buf_send),0);
printf("\n\n receiving the string \"File found or not\"\n\n");
n = recv(sd, buf_recv, sizeof(buf_recv), 0);
buf_recv[n]='\0';
printf("%s",buf_recv);
fflush(stdout);
printf("\n\n Receiving the file content \n\n");
n=recv(sd, file_buffer, sizeof(file_buffer), 0);
file_buffer[n]='\0';
fflush(stdout);