recv function and writing lines to a file

I am writing the client side to a TCP program where the client requests a file from a server and the server sends the file in packets one line at a time. I am having trouble using the receive function, and i believe i am using writing to the file incorrectly by using ofstream. Those lines 88 and 94 give errors.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include <netdb.h>
//#include <sys/socket.h>
//#include <netinet/in.h>
#include <unistd.h>
#include <iostream>
#include <winsock.h>

#define STRING_SIZE 1024

using namespace std;

int main(void) {

	int sock_client;   	//Socket used by client

	struct sockaddr_in server_addr;    // Internet address structure that stores server address

	struct hostent * server_hp;        // structure to store server's ip  address

	char server_hostname[STRING_SIZE] = "cisc450.cis.udel.edu";		//server host name

	unsigned short server_port = 45054;				//port number used by server (remote port)



	unsigned int name_len;					//length of file name

	int bytes_sent, bytes_recd;				//number of bytes sent or received from packet header

	char fileTransferred[STRING_SIZE];		//name of file being transferred

	int totalPacketsRecd = 0;					// total number of packets received

	int totalDataBytesReceived = 0;				// all counts summed up

	struct packet {
		int sequenceNumber;
		int count;
		char dataBytes[STRING_SIZE];
	};


	packet packetRecd;


	cout << "Enter name of file to be transfered" << endl;
	cin >> fileTransferred;

	name_len = strlen(fileTransferred) + 1;


	//open a socket

	if((sock_client= socket(PF_INET, SOCK_STREAM, IPPROTO_UDP)) < 0) {
		perror("Client: cant open stream socket");
		exit(1);
	}


	


	// clear server address structure and initalize with server address

	memset(&server_addr, 0, sizeof(server_addr));
	server_addr.sin_family = AF_INET;
	memcpy((char *)&server_addr.sin_addr,server_hp->h_addr,server_hp->h_length);
	server_addr.sin_port = htons(server_port);

	//connect to the server

	if (connect(sock_client, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0) {
		perror("Client: can't connect to server");
		close(sock_client);
		exit(1);
	}

	//send message

	bytes_sent = send(sock_client, fileTransferred, name_len, 0);

	// get response from server

	while(packetRecd.count != 0) {
	bytes_recd = recv(sock_client, &packetRecd, STRING_SIZE, 0);

	cout <<  "Packet " << packetRecd.sequenceNumber <<
			"received with" << packetRecd.count << "data bytes";


	ofstream out ("out.txt");
	cin >> packetRecd.dataBytes;

	cout << "The response from server is:" << endl;
	cout << packetRecd.dataBytes;

	totalPacketsRecd++;
	totalDataBytesReceived = totalDataBytesReceived + packetRecd.count;
	}

	totalPacketsRecd = totalPacketsRecd + 1; 	// for termination packet

	cout << "End of Transmission Packet with sequence number " << packetRecd.sequenceNumber
			<< "received with " << packetRecd.count << "data bytes";



	//close the socket

	close(sock_client);



}
Last edited on
ofstream discards the old file content by default. You could either reuse the same ofstream object during the whole time, or you could pass std::ios::app as argument to the constructor.
 
ofstream out ("out.txt", ios::app);
Topic archived. No new replies allowed.