Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

client/server file transfer, ERROR in receive files

Status
Not open for further replies.

Negneg

Programmer
Dec 29, 2010
10
Hi everyone,

I have written a client-server program in which client sends file to server and server must receive..but I get Receive ERROR (Can not receive DATAGRAM). could anyone tell me whats wrong with my codes?!


here is my server codes:

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock.h>
#include <time.h>
#include <iostream>
#include <tchar.h>
#include <fstream>
#include <io.h>

#define BUFFER_SIZE 4096

void usage(void);


int main(int argc, char **argv)
{
WSADATA w; /* Used to open windows connection */
// unsigned short port_number; /* Port number to use */
//int a1, a2, a3, a4; /* Components of address in xxx.xxx.xxx.xxx form */
int client_length; /* Length of client struct */
int bytes_received; /* Bytes received from client */
SOCKET sd; /* Socket descriptor of server */
struct sockaddr_in server; /* Information about the server */
struct sockaddr_in client; /* Information about the client */
//char buffer[BUFFER_SIZE]; /* Where to store received data */
struct hostent *hp; /* Information about this computer */
char host_name[256]; /* Name of the server */
char line[2500];
//time_t current_time; /* Current time */



/* Open windows connection */
if (WSAStartup(0x0101, &w) != 0)
{
fprintf(stderr, "Could not open Windows connection.\n");
exit(0);
}

/* Open a datagram socket */
sd = socket(AF_INET, SOCK_DGRAM, 0);
if (sd == INVALID_SOCKET)
{
fprintf(stderr, "Could not create socket.\n");
WSACleanup();
exit(0);
}

/* Clear out server struct */
memset((void *)&server, '\0', sizeof(struct sockaddr_in));

/* Set family and port */
server.sin_family = AF_INET;
server.sin_port = htons(0);

/* Set address automatically if desired */
if (argc == 2)
{
/* Get host name of this computer */
gethostname(host_name, sizeof(host_name));
hp = gethostbyname(host_name);
}



/* assign th address manually */

server.sin_addr.S_un.S_un_b.s_b1 = (unsigned char)201;
server.sin_addr.S_un.S_un_b.s_b2 = (unsigned char)201;
server.sin_addr.S_un.S_un_b.s_b3 = (unsigned char)201;
server.sin_addr.S_un.S_un_b.s_b4 = (unsigned char)140;
//}

/* Bind address to socket */
if (bind(sd, (struct sockaddr *)&server, sizeof(struct sockaddr_in)) == -1)
{
fprintf(stderr, "Could not bind name to socket.\n");
closesocket(sd);
WSACleanup();
exit(0);
}

/* Print out server information */
printf("Server running on %u.%u.%u.%u\n", (unsigned char)server.sin_addr.S_un.S_un_b.s_b1,
(unsigned char)server.sin_addr.S_un.S_un_b.s_b2,
(unsigned char)server.sin_addr.S_un.S_un_b.s_b3,
(unsigned char)server.sin_addr.S_un.S_un_b.s_b4);
printf("Press CTRL + C to quit\n");

/* Loop and get data from clients */
while (1)
{


FILE *fd2,*fd3;
char line[2500];

if ((fd2 = fopen ("Come_from_Client.py" , "w+t")) ==NULL)
printf ("File can not be opened.\n");

fclose(fd2);

client_length = (int)sizeof(struct sockaddr_in);

/* Receive bytes from client */
bytes_received = recvfrom(sd, line, 2500, 0, (struct sockaddr *)&client, &client_length);
if (bytes_received < 0)
{
fprintf(stderr, "Could not receive datagram.\n");
closesocket(sd);
WSACleanup();
exit(0);
}
else
{ if ((fd3 = fopen ("Come_from_Client.py" , "w+t")) ==NULL)
{
printf ("File can not be opened.\n");
}
else
{
fwrite(line,sizeof(char),2500,fd2);
}
fclose (fd3);
}
}


closesocket(sd);
WSACleanup();

return 0;
}

void usage(void)
{
fprintf(stderr, "timeserv [server_address] port\n");
exit(0);



Client Code:

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <winsock.h>

#define SIZE 500

void usage(void);

int main(int argc, char **argv)
{
WSADATA w; /* Used to open Windows connection */
SOCKET sd; /* The socket descriptor */
int server_length; /* Length of server struct */
struct sockaddr_in server; /* Information about the server */
struct sockaddr_in client; /* Information about the client */
char host_name[256]; /* Host name of this computer */
char line[2500]; /* Used to store file content */
char FileName[20]; /* defines FileName as a charachter*/



/* Open windows connection */
if (WSAStartup(0x0101, &w) != 0)
{
fprintf(stderr, "Could not open Windows connection.\n");
exit(0);
}

/* Open a datagram socket */
sd = socket(AF_INET, SOCK_DGRAM, 0);
if (sd == INVALID_SOCKET)
{
fprintf(stderr, "Could not create socket.\n");
WSACleanup();
exit(0);
}

/* Clear out server struct */
memset((void *)&server, '\0', sizeof(struct sockaddr_in));

/* Set family and port */
server.sin_family = AF_INET;
server.sin_port = htons(0);

/* Set server address */


server.sin_addr.S_un.S_un_b.s_b1 = 201;
server.sin_addr.S_un.S_un_b.s_b2 = 201;
server.sin_addr.S_un.S_un_b.s_b3 = 201;
server.sin_addr.S_un.S_un_b.s_b4 = 140;

/* Clear out client struct */
memset((void *)&client, '\0', sizeof(struct sockaddr_in));

/* Set family and port */
client.sin_family = AF_INET;
client.sin_port = htons(0);

client.sin_addr.S_un.S_un_b.s_b1 = 201;
client.sin_addr.S_un.S_un_b.s_b2 = 201;
client.sin_addr.S_un.S_un_b.s_b3 = 201;
client.sin_addr.S_un.S_un_b.s_b4 = 140;


/* Get host name of this computer */
gethostname(host_name, sizeof(host_name));
hp = gethostbyname(host_name);



/* Bind local address to socket */
if (bind(sd, (struct sockaddr *)&client, sizeof(struct sockaddr_in)) == -1)
{
fprintf(stderr, "Cannot bind address to socket.\n");
closesocket(sd);
WSACleanup();
exit(0);
}



printf( "Please enter Filename to send: ");
scanf( "%s", FileName );
printf( "Filename Is: %s\n", FileName);
FILE *fd1;
if ((fd1=fopen(FileName, "r"))== NULL){
printf ("ERROR:File can not be opened. Please key-in a correct File name.\n");
}
else
{
fread(line,sizeof(char),2500,fd1);
int len=strlen(line);
if (line[len] == '\n')
line[len]=0;
printf("\n%s", line);
}

/* Send file to server */
server_length = sizeof(struct sockaddr_in);
if (sendto(sd, line, (int)strlen(line) + 1, 0, (struct sockaddr *)&server, server_length) == -1)
{
fprintf(stderr, "Error transmitting data.\n");
closesocket(sd);
WSACleanup();
exit(0);
}
fclose(fd1);


printf("DATA HAS BEEN SENT");
closesocket(sd);
WSACleanup();

return 0;
}

void usage(void)
{
fprintf(stderr, "Usage: timecli server_address port [client_address]\n");
exit(0);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top