By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,107 Members | 714 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,107 IT Pros & Developers. It's quick & easy.

syntax error near unexpected token `('

P: 3
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
Mar 24 '08 #1
Share this Question
Share on Google+
3 Replies

Expert 100+
P: 849
Please only post one thread per question. Secondly, please use the provided CODE tags when posting code.

When I c/p your code onto my computer, it compiles just fine. I'm using gcc 4.1.3 on Ubuntu and used no compilation flags other than -o.
Mar 24 '08 #2

P: 3
nvr
Please only post one thread per question. Secondly, please use the provided CODE tags when posting code.

When I c/p your code onto my computer, it compiles just fine. I'm using gcc 4.1.3 on Ubuntu and used no compilation flags other than -o.
when i have run the program using he -o i got the following errors

please help
Clientsend.c:1:19: error: stdio.h: No such file or directory
Clientsend.c:2:20: error: stdlib.h: No such file or directory
Clientsend.c:3:20: error: string.h: No such file or directory
Clientsend.c:4:23: error: sys/types.h: No such file or directory
Clientsend.c:5:24: error: sys/socket.h: No such file or directory
Clientsend.c:6:24: error: netinet/in.h: No such file or directory
Clientsend.c:7:23: error: arpa/inet.h: No such file or directory
Clientsend.c:9:22: error: sys/stat.h: No such file or directory
Clientsend.c:10:20: error: unistd.h: No such file or directory
Clientsend.c: In function ‘main’:
Clientsend.c:17: error: ‘int32_t’ undeclared (first use in this function)
Clientsend.c:17: error: (Each undeclared identifier is reported only once
Clientsend.c:17: error: for each function it appears in.)
Clientsend.c:17: error: expected ‘;’ before ‘i32SocketFD’
Clientsend.c:18: error: storage size of ‘stSockAddr’ isn’t known
Clientsend.c:20: error: storage size of ‘st’ isn’t known
Clientsend.c:21: error: ‘FILE’ undeclared (first use in this function)
Clientsend.c:21: error: ‘fp’ undeclared (first use in this function)
Clientsend.c:25: warning: incompatible implicit declaration of built-in function ‘printf’
Clientsend.c:27: warning: incompatible implicit declaration of built-in function ‘scanf’
Clientsend.c:34: warning: incompatible implicit declaration of built-in function ‘exit’
Clientsend.c:45: error: ‘NULL’ undeclared (first use in this function)
Clientsend.c:48: warning: incompatible implicit declaration of built-in function ‘exit’
Clientsend.c:52: error: ‘i32SocketFD’ undeclared (first use in this function)
Clientsend.c:52: error: ‘PF_INET’ undeclared (first use in this function)
Clientsend.c:52: error: ‘SOCK_STREAM’ undeclared (first use in this function)
Clientsend.c:56: warning: incompatible implicit declaration of built-in function ‘exit’
Clientsend.c:60: warning: incompatible implicit declaration of built-in function ‘bzero’
Clientsend.c:62: error: ‘AF_INET’ undeclared (first use in this function)
Clientsend.c:64: error: ‘i32Res’ undeclared (first use in this function)
Clientsend.c:70: warning: incompatible implicit declaration of built-in function ‘exit’
Clientsend.c:76: warning: incompatible implicit declaration of built-in function ‘exit’
Clientsend.c:81: warning: incompatible implicit declaration of built-in function ‘exit’
Clientsend.c:89: warning: incompatible implicit declaration of built-in function ‘exit’
Clientsend.c:93: warning: incompatible implicit declaration of built-in function ‘malloc’
Clientsend.c:97: warning: incompatible implicit declaration of built-in function ‘exit’
Clientsend.c:117: warning: incompatible implicit declaration of built-in function ‘exit’
Mar 24 '08 #3

Expert 100+
P: 849
Ummm...all I can think to tell you is check in /usr/include to see if the files are actually there or not. If they're not, you might try reinstalling gcc.
Mar 24 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.