ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi my friends;
this is my client and server. I can receive text file. but I can't send other type of file like PDF,Docx,Odt and ...
what's the problem?
Server:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main ( int agrc, char *argv[] )
{
/******** Program Variable Define & Initialize **********/
int Main_Socket; // Main Socket For Server
int Communication_Socket; // Socket For Special Clients
int Status; // Status Of Fucntion
struct sockaddr_in Server_Address; // Address Of Server
struct sockaddr_in Client_Address; // Address Of Client That Communicate Whit Server
int Port ;
char Buff[100] = "";
Port = atoi(argv[2]);
printf ("Server Communicating By Using Port %d\n", Port);
/******** Create A Socket To Communicate With Server **********/
Main_Socket = socket ( AF_INET, SOCK_STREAM, 0 );
if ( Main_Socket == -1 )
{
printf ("Sorry System Can Not Create Socket!\n");
}
/******** Create A Address For Server To Communicate **********/
Server_Address.sin_family = AF_INET;
Server_Address.sin_port = htons(Port);
Server_Address.sin_addr.s_addr = inet_addr(argv[1]);
/******** Bind Address To Socket **********/
Status = bind ( Main_Socket, (struct sockaddr*)&Server_Address, sizeof(Server_Address) );
if ( Status == -1 )
{
printf ("Sorry System Can Not Bind Address to The Socket!\n");
}
/******** Listen To The Port to Any Connection **********/
listen (Main_Socket,12);
/******** Waiting For Connection **********/
socklen_t Lenght = sizeof (Client_Address);
while (1)
{
Communication_Socket = accept ( Main_Socket, (struct sockaddr*)&Client_Address, &Lenght );
if (!fork())
{
FILE *fp=fopen("sample","w");
while(1)
{
char Buffer[2]="";
if (recv(Communication_Socket, Buffer, sizeof(Buffer), 0))
{
if ( strcmp (Buffer,"Hi") == 0 )
{
break;
}
else
{
fputs(Buffer,fp);
}
}
}
send(Communication_Socket, "ACK" ,3,0);
printf("ACK Send");
exit(0);
}
}
return 0;
}
Client:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main ( int agrc, char *argv[] )
{
int Socket;
struct sockaddr_in Server_Address;
Socket = socket ( AF_INET, SOCK_STREAM, 0 );
if ( Socket == -1 )
{
printf ("Can Not Create A Socket!");
}
int Port ;
Port = atoi(argv[2]);
Server_Address.sin_family = AF_INET;
Server_Address.sin_port = htons ( Port );
Server_Address.sin_addr.s_addr = inet_addr(argv[1]);
if ( Server_Address.sin_addr.s_addr == INADDR_NONE )
{
printf ( "Bad Address!" );
}
connect ( Socket, (struct sockaddr *)&Server_Address, sizeof (Server_Address) );
FILE *in = fopen("Scan","r");
char Buffer[2] = "";
int len;
while ((len = fread(Buffer,sizeof(Buffer),1, in)) > 0)
{
send(Socket,Buffer,sizeof(Buffer),0);
}
send(Socket,"Hi",sizeof(Buffer),0);
char Buf[BUFSIZ];
recv(Socket, Buf, BUFSIZ, 0);
//printf("%s\n",Buf);
if ( strcmp (Buf,"ACK") == 0 )
{
printf("Recive ACK\n");
}
close (Socket);
fclose(in);
return 0;
}
I can receive text file. But I can't send other types of files. What's the problem?
Your code has several problems. I pointed them out using comments inline below.
Quote:
Originally Posted by htabesh
Server:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main ( int agrc, char *argv[] )
{
/******** Program Variable Define & Initialize **********/
int Main_Socket; // Main Socket For Server
int Communication_Socket; // Socket For Special Clients
int Status; // Status Of Fucntion
struct sockaddr_in Server_Address; // Address Of Server
struct sockaddr_in Client_Address; // Address Of Client That Communicate Whit Server
int Port ;
char Buff[100] = "";
Port = atoi(argv[2]);
printf ("Server Communicating By Using Port %d\n", Port);
/******** Create A Socket To Communicate With Server **********/
Main_Socket = socket ( AF_INET, SOCK_STREAM, 0 );
if ( Main_Socket == -1 )
{
printf ("Sorry System Can Not Create Socket!\n");
}
/******** Create A Address For Server To Communicate **********/
Server_Address.sin_family = AF_INET;
Server_Address.sin_port = htons(Port);
Server_Address.sin_addr.s_addr = inet_addr(argv[1]);
/******** Bind Address To Socket **********/
Status = bind ( Main_Socket, (struct sockaddr*)&Server_Address, sizeof(Server_Address) );
if ( Status == -1 )
{
printf ("Sorry System Can Not Bind Address to The Socket!\n");
}
/******** Listen To The Port to Any Connection **********/
//------------------------------------------------------
// listen can fail. You need to check the return value.
//------------------------------------------------------
listen (Main_Socket,12);
/******** Waiting For Connection **********/
socklen_t Lenght = sizeof (Client_Address);
while (1)
{
//------------------------------------------------------
// accept can fail. You need to check the return value.
//------------------------------------------------------
Communication_Socket = accept ( Main_Socket, (struct sockaddr*)&Client_Address, &Lenght );
if (!fork())
{
//-----------------------------------------------------
// fopen can fail. You need to check the return value.
//-----------------------------------------------------
FILE *fp=fopen("sample","w");
while(1)
{
char Buffer[2]="";
//-----------------------------------------------
// You need to check the return value here.
// recv can return -1, which qualifies as true
// according to the rules for truth in C.
//-----------------------------------------------
if (recv(Communication_Socket, Buffer, sizeof(Buffer), 0))
{
//--------------------------------------------
// You are using strcmp on a buffer that will
// not be NULL terminated. Either use memcmp,
// or change the buffer length above to 3.
//--------------------------------------------
if ( strcmp (Buffer,"Hi") == 0 )
{
break;
}
else
{
//----------------------------------------
// This needs to be an fwrite. fputs
// stops when it reaches a '\0' character.
// Binary files are filled with '\0'
// characters, so data will be lost.
//----------------------------------------
fputs(Buffer,fp);
}
}
}
//----------------------------------------------------
// send can fail. You need to check the return value.
//----------------------------------------------------
send(Communication_Socket, "ACK" ,3,0);
printf("ACK Send");
exit(0);
}
}
return 0;
}
Last edited by David1357; 06-22-2009 at 12:19 PM.
Reason: Fix erroneous statement about the behaviour of fputs.
you don't need to know the size.
recv reads size bytes into the buffer.
if it's not big enough you go round again in a loop.
you can use realloc to make a flexible buffer.
read the man pages.
what are you doing with the data?
do you need to save it all?
Then you can declare a message on the stack like this:
Code:
int bogus(void)
{
MESSAGE Message;
Message.Header.iType = 1;
Message.Header.iSize = sizeof(Message);
strcpy(Message.Data, "FileName.txt");
// Send your message somewhere
}
As long as your server and client use the same definitions for all the structures, everything will work. You can use the "iType" and "iSize" fields to determine whether or not the received data is a file name message or a data message.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main ( int agrc, char *argv[] )
{
int Socket;
struct sockaddr_in Server_Address;
Socket = socket ( AF_INET, SOCK_STREAM, 0 );
if ( Socket == -1 )
{
printf ("Can Not Create A Socket!");
}
int Port ;
Port = atoi(argv[2]);
Server_Address.sin_family = AF_INET;
Server_Address.sin_port = htons ( Port );
Server_Address.sin_addr.s_addr = inet_addr(argv[1]);
if ( Server_Address.sin_addr.s_addr == INADDR_NONE )
{
printf ( "Bad Address!" );
}
connect ( Socket, (struct sockaddr *)&Server_Address, sizeof (Server_Address) );
char *path;
char *filename;
path = "/home/hosein/Desktop/Project-30-3/Scan.pdf";
filename = strrchr(path, '/') + 1;
send(Socket,filename,sizeof(filename),0);
FILE *in = fopen("1.tar.gz","r");
char Buffer[2] = "";
int len;
while ((len = fread(Buffer,sizeof(Buffer),1, in)) > 0)
{
send(Socket,Buffer,sizeof(Buffer),0);
}
send(Socket,"Hi",sizeof(Buffer),0);
char Buf[BUFSIZ];
recv(Socket, Buf, BUFSIZ, 0);
if ( strcmp (Buf,"ACK") == 0 )
{
printf("Recive ACK\n");
}
close (Socket);
fclose(in);
return 0;
}
Server:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main ( int agrc, char *argv[] )
{
/******** Program Variable Define & Initialize **********/
int Main_Socket; // Main Socket For Server
int Communication_Socket; // Socket For Special Clients
int Status; // Status Of Fucntion
struct sockaddr_in Server_Address; // Address Of Server
struct sockaddr_in Client_Address; // Address Of Client That Communicate Whit Server
int Port ;
char Buff[100] = "";
Port = atoi(argv[2]);
printf ("Server Communicating By Using Port %d\n", Port);
/******** Create A Socket To Communicate With Server **********/
Main_Socket = socket ( AF_INET, SOCK_STREAM, 0 );
if ( Main_Socket == -1 )
{
printf ("Sorry System Can Not Create Socket!\n");
}
/******** Create A Address For Server To Communicate **********/
Server_Address.sin_family = AF_INET;
Server_Address.sin_port = htons(Port);
Server_Address.sin_addr.s_addr = inet_addr(argv[1]);
/******** Bind Address To Socket **********/
Status = bind ( Main_Socket, (struct sockaddr*)&Server_Address, sizeof(Server_Address) );
if ( Status == -1 )
{
printf ("Sorry System Can Not Bind Address to The Socket!\n");
}
/******** Listen To The Port to Any Connection **********/
listen (Main_Socket,12);
socklen_t Lenght = sizeof (Client_Address);
while (1)
{
Communication_Socket = accept ( Main_Socket, (struct sockaddr*)&Client_Address, &Lenght );
if (!fork())
{
char CH[4];
recv(Communication_Socket, CH, sizeof(CH), 0);
printf("%s\n",CH);
FILE *fp=fopen("sample.tar.gz","w");
while(1)
{
char Buffer[2]="";
if (recv(Communication_Socket, Buffer, sizeof(Buffer), 0))
{
if ( strcmp (Buffer,"Hi") == 0 )
{
break;
}
else
{
fwrite(Buffer,sizeof(Buffer),1, fp);
}
}
}
fclose(fp);
send(Communication_Socket, "ACK" ,3,0);
printf("ACK Send");
exit(0);
}
}
return 0;
}
for example if I declare CH[100], there will be problem to receive file.
whit CH[8] everything work right.
I'm sorry that your problem was not solved. From what I understand, you are attempting to send a string (e.g. a filename) from a client to a server, and then afterwards the client will send the file's data content.
Here's a synopsis of what the server COULD look like:
Code:
void handleClient(const int sd);
int main(int argc, char** argv)
{
int listen_sd = socket(AF_INET, SOCK_STREAM, 0);
// ...
bind(listen_sd, (struct sockaddr*) &addr, sizeof(addr));
listen(listen_sd, 5);
for (;;)
{
int client_sd = accept(listen_sd, 0, 0);
if (client_sd > 0 && fork() == 0)
{
handleClient(client_sd);
exit(0);
}
}
close(listen_sd);
}
void handleClient(const int sd)
{
fd_set savefds;
FD_ZERO(&savefds);
FD_SET(sd, &savefds);
enum State { WAITING_FOR_FILENAME, WAITING_FOR_DATA };
State state = WAITING_FOR_FILENAME;
FILE* file = 0;
int done = 0;
while (!done)
{
fd_set readfds = savefds;
int sel = select(sd, &readfds, 0, 0, 0);
if (sel <= 0)
{
done = 1;
continue;
}
char buf[1024] = {0};
int rtn = read(sd, buf, sizeof(buf), 0);
if (rtn <= 0)
{
if (file)
{
fflush(file);
fclose(file);
}
done = 1;
continue;
}
if (state == WAITING_FOR_FILENAME)
{
file = fopen(buf, "w+");
if (file)
{
state = WAITING_FOR_DATA;
}
else
{
fprintf(stderr, "Cannot open file %s\n", buf);
done = 1;
}
}
else // WAITING_FOR_DATA
{
fwrite(buf, rtn, 1, file);
}
}
close(sd);
}
Sorry for the lack of comments; I hope it is clear what is going on. Also, I did not compile/test this, so forgive me if there is a minor issue.
As for the client, something like:
Code:
int main(int argc, char** argv)
{
int sd = socket(AF_INET, SOCK_STREAM, 0);
// ...
int rtn = connect(sd, (struct sockaddr*) &addr, sizeof(addr));
if (rtn != 0)
{
perror("Failed to connect to the server.");
return 1;
}
const char* filename = "Foo.txt";
if (send(sd, filename, strlen(filename), 0) == strlen(filename))
{
FILE* file = fopen(filename, "r");
if (file)
{
char buf[1024] = {0};
int bytes = 0;
while ((bytes = fread(buf, sizeof(buf), 1, file)) > 0)
{
send(sd, buf, bytes, 0);
}
}
}
}
Once again, I have not tested this code, but it is logically laid out to perform the task you require.
You may want to consider having the client/server perform some sort of hand-shaking, so that ACK/NAK messages can be sent from the server to the client to indicate success/failure with the request; you never know, the server may encounter an error.
Last edited by dwhitney67; 06-23-2009 at 12:58 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.