Hi all
I am doing the socket programming for the client side. but the code is not compiling and i am getting the below error
./Clientsend.c: line 11: syntax error near unexpected token `('
./Clientsend.c: line 11: `int main()'
Code:
#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 <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int main()
{
int ret, sendVal, left_bytes;
char fileName[200];
int fileReadSize = 4000;
char *buf;
int32_t i32SocketFD, i32Res;
struct sockaddr_in stSockAddr;
int fileSize;
struct stat st;
FILE* fp;
//File name input
printf("\nEnter the name of the file to be sent:\n");
scanf("%s",&fileName);
printf("\nThe name of the file is: %s\n",fileName);
//Fill stat struct with file info
if(lstat (fileName, &st) != 0)
{
printf("file info attempt unsuccessful!!!");
exit(-1);
}//end if
//Get file size and store in file size
fileSize = st.st_size;
//output file size
printf("The file Size is %d",fileSize);
//Open file to be read
if((fp = fopen(fileName,"r") ) == NULL)
{
printf("fopen FAILURE\n");
exit(-1);
}//end if
//Open Socket and retrieve socket descriptor
i32SocketFD = socket(PF_INET,SOCK_STREAM,0);
if(-1 == i32SocketFD)
{
printf("Cannot create socket");
exit(-1);
}//end if
//-------------------------Socket variable filling--------------------
bzero(&stSockAddr, sizeof(stSockAddr));
stSockAddr.sin_family = AF_INET;
stSockAddr.sin_port = htons(10000);
i32Res = inet_pton(AF_INET, "138.250.9.29",(void*)&stSockAddr.sin_addr);
if(0 > i32Res)
{
printf("Error: First Parameter is not a valid address family");
exit(-1);
}
if(0 == i32Res)
{
printf("char string second parameter does not contain valid ipaddress");
exit(-1);
}
if(-1 == connect(i32SocketFD,(struct sockaddr*)&stSockAddr,sizeof(stSockAddr)))
{
printf("Connection Failed.......");
exit(-1);
}
//send file size to server
sendVal = send(i32SocketFD, &fileSize, sizeof(int),0);
if(sendVal == -1 )
{
printf("Invalid sending...");
exit(-1);
}
//Allocate memory to store file to be sent
buf = (char *) malloc (fileReadSize);
if (buf == NULL)
{
printf ("Malloc for buf failed");
exit (-1);
}
//set no of files left to file size so we can decreament from file size
left_bytes = fileSize;
if (fileReadSize > left_bytes) fileReadSize = left_bytes;
//Read and send file
while ((ret = fread(buf, fileReadSize, 1, fp)) >= 0)
{
int cnt = 0;
//send file take note of buf+cnt and fileReadSize-cnt
while (cnt < fileReadSize)
{
sendVal = send(i32SocketFD, buf + cnt, fileReadSize - cnt, 0);
if (sendVal < 0)
{
printf ("send eroor\n");
exit (-1);
}
//increment cnt based on send val
cnt += sendVal;
}
//reading and sending final bits of the file --- while file is coming to end
left_bytes -= fileReadSize;
if (left_bytes <= 0) break;
if (fileReadSize > left_bytes) fileReadSize = left_bytes;
printf("\nScanning for file size...");
}//end of while loop
//----------Socket shutting and closing------------------
shutdown(i32SocketFD,2);
close(i32SocketFD);
fclose(fp);
return 0;
}
Please help me. i have to submit the assignment. So please help me asap
Regards
Neeraja