Disclaimer: I'm googling my way through this
Using C++
Both ends are Linux
I can see the SSH connection on the far end via TCPDUMP
No build errors or return codes other than 0 (debug says its all good)
TCPDUMP doesn't look like its transferring enough data (it is a 2MB file*)
Connect / Authenticate / Authorize are happening properly as far as I can tell
When i go to the far end looking for the file that was supposedly copied over, it doesn't exist. I've tried updatedb & locate "file", but it's nowhere to be found.
Thank you in advance for any insight possible!
SSH Functions (auth & connect) cleared out for "Minimal, Complete, and Verifiable example" since they seem to be working fine*
#include <cstdio>
#include <string>
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <curl/curl.h>
#include <boost/regex.hpp>
#include <boost/system/error_code.hpp>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <x86_64-linux-gnu/sys/stat.h>
#define LIBSSH_STATIC 1
#include <libssh/libssh.h>
using namespace std;
int sourcePull(void)
{
CURL *curl;
FILE *fp;
CURLcode res;
char *url = "https://raw.githubusercontent.com/firehol/blocklist-ipsets/master/firehol_level1.netset";
char outfilename[FILENAME_MAX] = "fireHOL";
curl = curl_easy_init();
if (curl)
{
fp = fopen(outfilename, "wb");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fp);
}
return 0;
}
int sourceParse()
{
//Regex for IP's in sourcePull result
ifstream infile("fireHOL");
string ipAddress;
boost::regex expr1("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$");
boost::regex expr2("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\\/([0-9]|[1-2][0-9]|3[0-2]))$");
boost::smatch matches;
while (infile >> ipAddress)
{
if (boost::regex_match(ipAddress, matches, expr1))
{
ofstream checkpoint;
checkpoint.open("samp_batch.txt", ios::out | ios::app);
checkpoint << "add -t 86400 -a d -l r -o icewall ip -s " << ipAddress << endl;
checkpoint << "add -t 86400 -a r -l r -o icewall ip -d " << ipAddress << endl;
}
if (boost::regex_match(ipAddress, matches, expr2))
{
ofstream checkpoint;
checkpoint.open("samp_batch.txt", ios::out | ios::app);
checkpoint << "add -t 86400 -a d -l r -o icewall ip -s " << ipAddress << endl;
checkpoint << "add -t 86400 -a r -l r -o icewall ip -d " << ipAddress << endl;
}
}
ofstream terminate;
terminate.open("samp_batch.txt", ios::out | ios::app);
terminate << "EOF";
return 0;
}
int fileSize()
{
int size;
streampos begin, end;
ifstream inputFile("samp_batch", ios::binary);
begin = inputFile.tellg();
inputFile.seekg(0, ios::end);
end = inputFile.tellg();
inputFile.close();
return size;
}
int ssh()
{
ssh_session my_ssh_session;
ssh_scp scp;
int port = 22;
int rc;
int method;
char password[128] = { 0 };
char *banner;
//open session & set options
my_ssh_session = ssh_new();
if (my_ssh_session == NULL)
exit(-1);
ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, "XXX.XXX.XXX.XXX");
ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &port);
ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, "security");
//connect to server
rc = ssh_connect(my_ssh_session);
if (rc != SSH_OK)
{
fprintf(stderr, "Error connecting to host: %s\n", ssh_get_error(my_ssh_session));
ssh_free(my_ssh_session);
exit(-1);
}
//verify the servers identity
if (verify_knownHost(my_ssh_session) < 0)
{
fprintf(stdout, "unkown host\n");
ssh_disconnect(my_ssh_session);
ssh_free(my_ssh_session);
exit(-1);
}
// Try to authenticate
rc = ssh_userauth_none(my_ssh_session, NULL);
if (rc == SSH_AUTH_ERROR) {
error(my_ssh_session);
return rc;
}
method = ssh_auth_list(my_ssh_session);
while (rc != SSH_AUTH_SUCCESS) {
// Try to authenticate with public key first
if (method & SSH_AUTH_METHOD_PUBLICKEY) {
rc = ssh_userauth_autopubkey(my_ssh_session, NULL);
if (rc == SSH_AUTH_ERROR) {
error(my_ssh_session);
return rc;
}
else if (rc == SSH_AUTH_SUCCESS) {
break;
}
}
return rc;
//SCP samp_batch file here
const int length = fileSize();
const char *fileName = "samp_batch";
ifstream inputFile(fileName);
constexpr size_t bufferSize = 1024 * 1024 * 1024;
unique_ptr<char[]> buffer(new char[bufferSize]);
const void* cvp = &buffer;
rc = ssh_scp_push_file(scp, "samp_batch", length, 0777);
if (rc != SSH_OK)
{
fprintf(stderr, "Can't open remote file: %s\n", ssh_get_error(my_ssh_session));
ssh_free(my_ssh_session);
exit(-1);
}
while (inputFile)
{
inputFile.read(buffer.get(), 1024 * 1024 * 1024);
rc = ssh_scp_write(scp, cvp, bufferSize);
if (rc != SSH_OK)
{
fprintf(stderr, "Cant write to remote file: %s\n", ssh_get_error(my_ssh_session));
ssh_free(my_ssh_session);
exit(-1);
}
}
return SSH_OK;
////execute remote command here
//ssh_free(my_ssh_session);
}
int main()
{
sourcePull();
sourceParse();
ssh();
return 0;
}
Solved this issue w/ of course, more reading...
I was not actually initializing the SCP subsystem "within" the SSH system.
By adding in;
scp = ssh_scp_new(my_ssh_session, SSH_SCP_WRITE, "/");
if (scp == NULL)
{
fprintf(stderr, "Error allocating scp session: %s\n", ssh_get_error(my_ssh_session));
return SSH_ERROR;
}
rc = ssh_scp_init(scp);
if (rc != SSH_OK)
{
fprintf(stderr, "Error initializing scp session: %s\n", ssh_get_error(my_ssh_session));
ssh_scp_free(scp);
return rc;
}
above my push/write functions, I was able to get the SCP subsystem to "start". Haven't gotten her functioning yet (SCP Status Code 1d not valid) error now, but i at least know scp is "awake" at this point...
Related
I'm using libssh to download a file from a Debian Linux server on my local Windows machine, the program is running without error but the downloaded file is corrupted. When I download via pscp it works, I don't know if I'm doing something wrong with regards to the buffer or something else.
The sizes of the generated files are identical, when I download it via other tools like FileZilla or WinSCP everything goes well. The file doesn't corrupt anymore. When I run my code then it gets corrupted
I'm running this code on Windows 10 with Visual Studio 2019. After messing with this code a lot I think the problem is with the write file writing function when I open the generated file in a hex editor I see differences in bytes only the end of the file that stays the same.
I ended up posting the Dart version of the code previously, but the C version is almost identical to the Dart version.
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_NONSTDC_NO_DEPRECATE
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <stdexcept>
#include <string.h>
#include <exception>
#include <fstream>
#include <libssh/callbacks.h>
#include <libssh/libssh.h>
#include <libssh/sftp.h>
#include "custom_exception.cpp"
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <io.h>
#include <time.h>
#include <chrono>
using std::chrono::high_resolution_clock;
using std::chrono::duration_cast;
using std::chrono::duration;
using std::chrono::milliseconds;
//using namespace std;
using std::string;
using std::cout;
using std::endl;
using std::cin;
// Good chunk size
#define MAX_XFER_BUF_SIZE 16384
int sftpDownloadFileTo(ssh_session session, const char* fullRemotePath, const char* fullLocalPath)
{
int access_type;
sftp_file file;
char buffer[MAX_XFER_BUF_SIZE * 8];//128KB
ssize_t nbytes, nwritten, rc;
int fd;
access_type = O_RDONLY;
sftp_session sftp = sftp_new(session);
if (sftp == NULL)
{
fprintf(stderr, "Error allocating SFTP session: %s\n",
ssh_get_error(session));
return SSH_ERROR;
}
rc = sftp_init(sftp);
if (rc != SSH_OK)
{
fprintf(stderr, "Error initializing SFTP session: %d.\n",
sftp_get_error(sftp));
sftp_free(sftp);
return rc;
}
file = sftp_open(sftp, fullRemotePath, access_type, 0);
if (file == NULL) {
fprintf(stderr, "Can't open file for reading: %s\n", ssh_get_error(session));
return SSH_ERROR;
}
fd = open(fullLocalPath, O_CREAT | O_RDWR, 0777);
if (fd < 0) {
fprintf(stderr, "Can't open file for writing: %s\n",
strerror(errno));
return SSH_ERROR;
}
for (;;) {
nbytes = sftp_read(file, buffer, sizeof(buffer));
if (nbytes == 0) {
break; // EOF
}
else if (nbytes < 0) {
fprintf(stderr, "Error while reading file: %s\n",
ssh_get_error(session));
sftp_close(file);
return SSH_ERROR;
}
nwritten = write(fd, buffer, nbytes);
if (nwritten != nbytes) {
fprintf(stderr, "Error writing: %s\n",
strerror(errno));
sftp_close(file);
return SSH_ERROR;
}
}
rc = sftp_close(file);
if (rc != SSH_OK) {
fprintf(stderr, "Can't close the read file: %s\n",
ssh_get_error(session));
return rc;
}
sftp_free(sftp);
return SSH_OK;
}
int main()
{
ssh_session my_ssh_session;
int rc;
int port = 22;
string password = "Ins257257";
auto host = "192.168.133.13";
auto username = "isaque.neves";
int verbosity = SSH_LOG_PROTOCOL;
// Abra a sessão e defina as opções
my_ssh_session = ssh_new();
if (my_ssh_session == NULL)
exit(-1);
ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, host);
//ssh_options_set(my_ssh_session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &port);
// Conecte-se ao servidor
rc = ssh_connect(my_ssh_session);
if (rc != SSH_OK)
{
fprintf(stderr, "Error connecting to host: %s\n",
ssh_get_error(my_ssh_session));
exit(-1);
}
// Autenticar-se
rc = ssh_userauth_password(my_ssh_session, username, password.c_str());
if (rc != SSH_AUTH_SUCCESS)
{
fprintf(stderr, "Error authenticating with password: %s\n",
ssh_get_error(my_ssh_session));
ssh_disconnect(my_ssh_session);
ssh_free(my_ssh_session);
exit(-1);
}
clock_t tic = clock();
auto t1 = high_resolution_clock::now();
sftpDownloadFileTo(my_ssh_session, "/home/isaque.neves/go1.11.4.linux-amd64.tar.gz", "D:/MyDartProjects/fsbackup/libssh_binding/go1.11.4.linux-amd64.tar.gz");
auto t2 = high_resolution_clock::now();
clock_t toc = clock();
duration<double, std::milli> ms_double = t2 - t1;
std::cout << ms_double.count() << " ms\r\n";
printf("Elapsed: %f seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC);
ssh_disconnect(my_ssh_session);
ssh_free(my_ssh_session);
std::cout << "End\n";
return 0;
}
https://github.com/insinfo/fsbackup/tree/main/libssh_binding
fd = open(fullLocalPath, O_CREAT | O_RDWR, 0777);
Here's your problem. Since you are running on Windows we need to adjust here.
fd = open(fullLocalPath, O_CREAT | O_RDWR | O_BINARY, 0777);
otherwise write() will try to adjust line endings.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <stddef.h>
#include <process.h>
#include <fcntl.h>
#include <sys\stat.h>
#include <fstream>
#define OK_IMAGE "HTTP/1.0 200 OK\r\nContent-Type:image/gif\r\n\r\n"
#define OK_TEXT "HTTP/1.0 200 OK\r\nContent-Type:text/html\r\n\r\n"
#define NOTOK_404 "HTTP/1.0 404 Not Found\r\nContent-Type:text/html\r\n\r\n"
#define MESS_404 "<html><body><h1>FILE NOT FOUND</h1></body></html>"
#define BUF_SIZE 1024
#define PORT_NUM 80
int Count;
void handle_get(void *in_arg);
void do_end(void *server_s);
void main()
{
unsigned int server_s; // Server socket descriptor
struct sockaddr_in server_addr; // Server Internet address
unsigned int client_s; // Client socket descriptor
struct sockaddr_in client_addr; // Client Internet address
int addr_len; // Internet address length
WSADATA wsaData;
WSAStartup(MAKEWORD(1,1), &wsaData);
server_s = socket(AF_INET, SOCK_STREAM, 0);
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT_NUM);
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
bind(server_s, (struct sockaddr *)&server_addr, sizeof(server_addr));
printf("HTTP Server Start.(Stop:ESC)\n");
if (_beginthread(do_end, 0, (void*)&server_s) < 0) {
printf("ERROR - Unable to create thread \n");
exit(1);
}
while(1)
{
listen(server_s, 50);
addr_len = sizeof(client_addr);
client_s = accept(server_s, (struct sockaddr*)&client_addr, addr_len);
if (client_s == -1)
{
printf("ERROR - Unable to create a socket \n");
exit(1);
}
Count++;
if (client_s == -1) {
if(server_s) printf("ERROR - Unable to create a socket \n");
exit(1);
}
if (_beginthread(handle_get, 0, (void *)client_s) < 0)
{
printf("ERROR - Unable to create a thread to handle the GET \n");
exit(1);
}
}
}
void handle_get(void *in_arg)
{
unsigned int client_s;
char in_buf[BUF_SIZE];
char out_buf[BUF_SIZE];
int fh;
int buf_len;
char command[BUF_SIZE];
char file_name[BUF_SIZE];
int retcode;
int i;
client_s = (unsigned int) in_arg;
retcode = recv(client_s, in_buf, BUF_SIZE, 0);
if (retcode <= 0)
{
printf("ERROR - Receive failed --- probably due to dropped connection \n");
Count--;
closesocket(client_s);
_endthread();
}
for (i=0; i<retcode; i++)
printf ("%c", in_buf[i]);
sscanf(in_buf, "%s %s \n", command, file_name);
if (strcmp(command, "GET") != 0) {
printf("ERROR - Not a GET --- received command = '%s' \n", command);
Count--;
closesocket(client_s);
_endthread();
}
if(file_name[1]=='\0') {
strcpy(file_name[1], "index.htm");
fh = open(file_name[1], O_RDONLY | O_BINARY, S_IREAD | S_IWRITE);
}
else
fh = open(&file_name[1], O_RDONLY | O_BINARY, S_IREAD | S_IWRITE);
if (fh == -1) {
printf("File '%s' not found --- sending an HTTP 404 \n", &file_name[1]);
strcpy(out_buf, NOTOK_404);
send(client_s, out_buf, strlen(out_buf), 0);
strcpy(out_buf, MESS_404);
send(client_s, out_buf, strlen(out_buf), 0);
Count--;
closesocket(client_s);
_endthread();
}
if (((file_name[1] == '.') && (file_name[2] == '.')) ||
(file_name[1] == '/') || (file_name[1] == '\\') ||
(file_name[2] == ':'))
{
printf("SECURITY VIOLATION --- trying to read '%s' \n", &file_name[1]);
Count--;
close(fh);
closesocket(client_s);
_endthread();
}
printf("Sending file '%s' \n\n\n", &file_name[1]);
if (strstr(file_name, ".gif") != NULL)
strcpy(out_buf, OK_IMAGE);
else
strcpy(out_buf, OK_TEXT);
send(client_s, out_buf, strlen(out_buf), 0);
while(!eof(fh)) {
buf_len = read(fh, out_buf, BUF_SIZE);
send(client_s, out_buf, buf_len, 0);
}
Count--;
close(fh);
closesocket(client_s);
_endthread();
}
void do_end(void *server_s)
{
unsigned int temp;
temp = *((unsigned int*)server_s);
while(getch()!=27);
while(Count);
*((unsigned int*)server_s) = 0;
printf("HTTP Server Stop.\n");
closesocket(temp);
WSACleanup();
exit(1);
This is a routine that allows the user to send the "index.htm" file first if the above if statement does not specify any file names. (If the current server has a file called "index.html" instead of this file, a problem occurs.)
file_name[1] contains the name of the requested file.
However, how can I provide a default page called "index.html" to a web browser if a file named "index.html" currently exists on the server and the user didn't name the file?
if(file_name[1]=='\0') {
strcpy(file_name[1], "index.htm");
fh = open(file_name[1], O_RDONLY | O_BINARY, S_IREAD | S_IWRITE);
}
Considering that it's Windows (given the #include <windows.h>), the easiest solution is to do nothing. index.html is found when opening index.htm, because the latter is the 8.3 name of index.html.
All you need to do is check the return value of sscanf(), which tells you how many tokens were read.
When I'm trying to run command on server (router) I get
[C] Jan 3 05:32:16 ndm: bin::ndmc: invalid option "-c".
Most of the code is taken from the documentation.
There is the code:
#include <iostream>
#include <string.h>
#include <libssh/libssh.h>
using std::string;
using std::cout;
using std::endl;
using std::cin;
string exec_ssh_command(ssh_session
session, char *command) {
string receive = "";
int rc, nbytes;
char buffer[256];
ssh_channel channel = ssh_channel_new(session);
if( channel == NULL )
return NULL;
rc = ssh_channel_open_session(channel);
if( rc != SSH_OK ) {
ssh_channel_free(channel);
return NULL;
}
rc = ssh_channel_request_exec(channel, command);
if( rc != SSH_OK ) {
ssh_channel_close(channel);
ssh_channel_free(channel);
cout << "Error";
return NULL;
}
nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
while (nbytes > 0)
{
if (write(1, buffer, nbytes) != (unsigned int) nbytes)
{
ssh_channel_close(channel);
ssh_channel_free(channel);
return NULL;
}
nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
}
if( nbytes < 0 )
return NULL;
ssh_channel_send_eof(channel);
ssh_channel_close(channel);
ssh_channel_free(channel);
return receive;
}
int main() {
ssh_session my_ssh_session = ssh_new();
if( my_ssh_session == NULL ) {
cout << "Error creating ssh session" << endl;
return 1;
}
ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, "192.168.1.1");
ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, "admin");
int rc = ssh_connect(my_ssh_session);
if( rc != SSH_OK ) {
cout << "Error with connecting" << endl;
ssh_free(my_ssh_session);
return -1;
}
rc = ssh_userauth_password(my_ssh_session, NULL, "pass");
if( rc != SSH_AUTH_SUCCESS) {
cout << "Error with authorization " << ssh_get_error(my_ssh_session) << endl;
ssh_disconnect(my_ssh_session);
ssh_free(my_ssh_session);
return -1;
}
string ip_hotspot = exec_ssh_command(my_ssh_session, "show ip hotspot");
cout << ip_hotspot << endl;
ssh_disconnect(my_ssh_session);
ssh_free(my_ssh_session);
return 0;
}
My router is zyxel keenetic ultra.
uname -a: Linux localhost 4.9.112-perf-gfb7749d #1 SMP PREEMPT Thu Dec 6 16:06:30 WIB 2018 aarch64 Android
g++ -v:
clang version 7.0.1 (tags/RELEASE_701/final)
Target: aarch64--linux-android
Thread model: posix
I'm a beginner at libssh so please explain easier. :)
Thank you in advance!
It was fixed. On version 3.3.2 of Keenetic OS it works fine.
I'm trying to download picture from website. The problem is that even tho I get all the content from HTTP response body, file won't open. I've been trying to solve this but I can't find the real problem. One thing I did notice is that picture downloaded using chromium displays different characters than picture downloaded from my code using command:
$ cat picture.png | less
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <fstream>
#include <iostream>
using std::cout;
using std::endl;
//with this function I remove HTTP header info, so I only get content.
char *removeHTTPHeader(char *buffer) {
char *t = strstr(buffer, "\r\n\r\n");
t = t + 4;
return t;
}
void getPicture(const int &socketfd, const int &bSize) {
std::ofstream file("picture.png",
std::ofstream::binary | std::ofstream::out);
char buffer[bSize];
ssize_t bReceived;
bReceived = recv(socketfd, buffer, bSize, 0);
char *t = removeHTTPHeader(buffer);
file.write(t, strlen(t));
memset(buffer, 0, bSize);
while ((bReceived = recv(socketfd, buffer, bSize, 0)) > 0) {
file.write(buffer, bReceived);
memset(buffer, 0, bSize);
}
file.close();
}
int main() {
int status;
addrinfo host_info;
addrinfo *host_info_list;
memset(&host_info, 0, sizeof(host_info));
host_info.ai_family = AF_UNSPEC;
host_info.ai_socktype = SOCK_STREAM;
host_info.ai_protocol = 0;
status = getaddrinfo("www.pngimg.com", "80", &host_info, &host_info_list);
if (status != 0) {
cout << "getaddrinfo error" << endl;
}
int socketfd;
socketfd = socket(host_info_list->ai_family, host_info_list->ai_socktype,
host_info_list->ai_protocol);
addrinfo *rp;
for (rp = host_info_list; rp != NULL; rp = rp->ai_next) {
socketfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (socketfd == -1) {
cout << "socket error" << endl;
}
if (connect(socketfd, rp->ai_addr, rp->ai_addrlen) != -1) {
break;
}
close(socketfd);
}
if (rp == NULL) {
cout << "Could not connect!" << endl;
exit(EXIT_FAILURE);
}
freeaddrinfo(host_info_list);
const char *msg =
"GET /upload/water_PNG3290.png HTTP/1.1\r\nhost: www.pngimg.com\r\nConnection: close\r\n\r\n";
status = send(socketfd, msg, strlen(msg), 0);
if (status == -1) {
cout << "error sending" << endl;
exit(EXIT_FAILURE);
}
getPicture(socketfd, 1024);
close(socketfd);
return 0;
}
Here is picture from using cat command:
terminal above is picture from my code, below is picture from chromium "save as"
Problem was that I didn't know that in C style string you can't do strlen on binary data. That's why I had to add counter in function removeHTTPHeader. Below are function getPicture and removeHTTPHeader that I've changed.
char *removeHTTPHeader(char *buffer, int &bodySize) {
char *t = strstr(buffer, "\r\n\r\n");
t = t + 4;
for (auto it = buffer; it != t; ++it) {
++bodySize;
}
return t;
}
void getPicture(const int &socketfd, const int &bSize) {
std::ofstream file("picture.png",
std::ofstream::binary | std::ofstream::out);
char buffer[bSize];
ssize_t bReceived;
bReceived = recv(socketfd, buffer, bSize, 0);
int bodySize = 0;
char *t = removeHTTPHeader(buffer, bodySize);
bodySize = bReceived - bodySize;
file.write(t, bodySize);
memset(buffer, 0, bSize);
while ((bReceived = recv(socketfd, buffer, bSize, 0)) > 0) {
file.write(buffer, bReceived);
memset(buffer, 0, bSize);
}
file.close();
}
i want to send a file from client to server through socket . yes it sends a file but the received file in the server is not full or complete like the original one.
So the test file originally has "this is a test" in it, and the received file has "this"
yes it's only 4 letters
i tried to change the original one becomes "MyMomGoesToTheMarket"
and received file has "MyMo" . still 4 letters which is not what i expect.
anyone know how to solve this problem and the solution ?
Here is the client :
#include "stdafx.h"
#include <WinSock2.h>
#include <Windows.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
using namespace std;
SOCKET clientsock;
WSADATA winsock;
sockaddr_in serverAddr , addr;
int Addrlen = sizeof(serverAddr);
FILE *File;
unsigned long Size;
void startClient() {
WSAStartup(MAKEWORD(2,2), &winsock);
if(LOBYTE(winsock.wVersion) != 2 || HIBYTE(winsock.wVersion) != 2 ){
WSACleanup();
}
clientsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
addr.sin_port = htons(6091);
connect(clientsock,(sockaddr*)&addr,sizeof(addr));
printf("socket connected... \n");
}
void sending() {
//preparing the file
ifstream myfile;
myfile.open("B:\RelativeLayout.txt",ios::in | ios::binary | ios::ate);
if(myfile.is_open()) {
printf("File open OK ! \n ");
}else {
printf("File not open ! \n ", WSAGetLastError());
}
//preparing the file size
long Size ;
myfile.seekg(0,fstream::end);
Size = myfile.tellg();
myfile.close();
printf("File Size : %d bytes.\n ",Size);
char cisi[10];
sprintf(cisi, "%i",Size);
send(clientsock,cisi,10,0); // file size sent
//sending the file
char *rbuffer;
myfile.open("B:\RelativeLayout.txt",ios::in | ios::binary | ios::ate);
if(myfile.is_open()) {
myfile.seekg(0, ios::beg);
rbuffer = new char[Size];
myfile.read(rbuffer, Size);
//send(clientsock, rbuffer, Size, 0);
int j = send(clientsock, rbuffer, Size, NULL); //send to server
if (j == -1){
cout << "Error sending file to server :(" << endl;
}else {
cout << " sending file to server succeed" << endl;
}
myfile.close();
}
}
int _tmain(int argc, _TCHAR* argv[])
{
startClient();
sending();
system("PAUSE");
return 0;
}
and here is the server code :
#include "stdafx.h"
#include <WinSock2.h>
#include <Windows.h>
#include <iostream>
#include <fstream>
using namespace std;
SOCKET servsocket, ClientAcc;
WSAData winsock;
sockaddr_in addr,incomingAddress;
int addrlen = sizeof(sockaddr_in);
int addresslen = sizeof(incomingAddress);
char *Filesize = new char[1024];
long Size;
void start() {
//socket initialization
WSAStartup(MAKEWORD(2,2), &winsock);
//socket check
if(LOBYTE(winsock.wVersion) !=2 || HIBYTE(winsock.wVersion) != 2 ) {
WSACleanup();
}
servsocket = socket(AF_INET,SOCK_STREAM, IPPROTO_TCP);
addr.sin_family = AF_INET;
addr.sin_port = htons(6091);
bind(servsocket, (sockaddr*)&addr, sizeof(addr));
listen(servsocket, 5);
ClientAcc = accept(servsocket, (sockaddr*)&incomingAddress, &addresslen);
char *ClientIP = inet_ntoa(incomingAddress.sin_addr);
int ClientPort = ntohs(incomingAddress.sin_port);
printf("Client Connected ... \n");
printf("IP : %s:%d\n", ClientIP, ClientPort);
}
void receiving() {
//receive the file size
recv(ClientAcc,Filesize,1024,0);
Size = atoi((const char*)Filesize);
printf("File size : %d\n",Size);
//receive the file
char *rbuffer;
rbuffer = new char[Size];
int k = recv(ClientAcc, rbuffer, sizeof(rbuffer), NULL);
if (k < 0){
cout << "Error uploading file" << endl;
}else {
fstream file;
file.open("B:\FileReceived.txt", ios::out|ios::binary| ios::ate);
file.write(rbuffer, sizeof(rbuffer));
file.close();
cout << "File received!" << endl;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
start();
receiving();
system("PAUSE");
return 0;
}
In your server code's recv function, you're passing in sizeof(rbuffer) as the amount of bytes to read in from the socket. rbuffer is a pointer however, and thus taking a sizeof it will return the size of a pointer on your architecture , which is typically 4 or 8 bytes, and since your server code is only reading 4 bytes, sizeof(rbuffer) would return 4 on your system.
To solve this, you need to pass in either Size-1 or strlen(rbuffer)-1 into the call to
int k = recv(ClientAcc, rbuffer, sizeof(rbuffer), NULL);
So it would look like this:
int k = recv(ClientAcc, rbuffer, Size-1, NULL);
This would actually read up to Size-1 bytes from the socket. You would then need to add the null terminator to the end of rbuffer.
rbuffer[k] = '\0';
Additionally, you need to make the same change in this line:
file.write(rbuffer, sizeof(rbuffer));
Which has the same problem as before - it only writes (in this case 4) bytes from rbuffer.