C++ :: Send Two Or More Char Arrays Over TCP Socket (winsock)
Nov 21, 2013
send(sConnect, (userinput, key), 256, 0);
sConnect is already predefined as a socket
Key is a character array of [32]
Userinput is a character array of [256]
How can you send both of them at the same time using the send function without having to create a separate connection?
View 1 Replies
ADVERTISEMENT
Oct 25, 2013
I'm trying to understand winsock with c++. Let's assume I have a 2 working applications, one is the client and one the server:
Client: I can enter a command, for example chat or filetransfer, it will then switch into this specific mode where I can enter commands like uploading a file, send a message etc.
Client
Code: ....
while (rc != SOCKET_ERROR) {
printf("
#");
gets(buf);
if (strcmp(buf, "CHAT") == 0){
// start chat mode
[Code] .....
I'm in a recv/send loop and I'm using streams...so basically all the data is being sent/received there. Now, if I want to upload a file, I send a FILETRANSFER String to the server. Then I will probably need another loop that receives file requests from the client. The server will need more details about the file, like the path, name, size...
Now, my question is, what's the best practise for something like that? I'm having problems understand how I can send 3 different values from the server to the client and how he will receive them in the right order store them in variables. And also, after sending something to the server, in some cases, the client will have to wait for the server to answer.
Is there a good example of a similar application?
View 3 Replies
View Related
Jun 28, 2014
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace test {
[Code] ....
I don't know if this is a bug in Winsock or the .NET Framework but i need to fix this or workaround. Recently I was working on a networking class and this error is breaking everything.
If you add Thread.Sleep(1) after Send() call then "TCP Bug detected" doesnt get executed but its not a reliable fix.
View 7 Replies
View Related
Jun 25, 2012
I have written a socket witch send an image 256x256 and now i want to send many images and i don't know what loop to write to make the socket after have sent the first image to send another and not to write the same program many times for every image ......
this is the part that read the binary image
if( !(fp=fopen("Anemos.bmp","rb"))) {
printf("Unable to load bin file: %s...
","Anemos.bmp");
[Code] ....
View 2 Replies
View Related
Mar 27, 2014
I am trying to make a streambuf for a socket, which will use either WinSock or POSIX sockets depending on OS. I understand about how to send and receive data via sockets, but I don't really understand how to put that into a 'streambuf'. What functions do I need to override?
View 7 Replies
View Related
Jul 28, 2013
when I want to use the same socket connected to the host to send() and recv() for the second time, recv() will return 0 without anything in buffer. Basically I am doing:
1. connect to the website
2. send packets
3. receive packets
4. send packets again (I think this one is working since it's not giving me SOCKET_ERROR)
5. receive packets again (this one is returning 0 so as "connection closed")
source code: [URL] ....
as you can see, I have sock2, which when I use for second send/recv it's working fine, however I will do more communication, and having sockets for all of that and connecting them.
View 4 Replies
View Related
Dec 26, 2014
I have Build code for sending/receiving TCP Traffic over c Socket . Now I want t add HTTP Protocol to it .
After searching out on google ,one method I found which is by GET method to send a query to a server like google.com and request a page .
But ,Suppose I saved a HTML page in my directory how can I send HTTP packet via client/server in socket using this HTML page .
View 4 Replies
View Related
Aug 6, 2014
I am trying to send yahoo mail using socket program.
Using the commands like ehlo, starttls, auth login, data,.,quit.
but starttls is error. yahoo close itself. one more thing i use this same method for gmail is working.
The problem is we must enable the ssl authentication. Like System.net mail in .net package smtp.enablessl=true. how i enable ssl authentication in starttls in c++ program
View 1 Replies
View Related
Dec 27, 2014
I have code for sending/receiving TCP traffic over C socket. Now I want to add HTTP protocol to it. After searching the web, one method I found which is by GET method to send a query to a server like google.com and request a page.
But, suppose I saved a HTML page in my directory how can I send HTTP packet via client/server in socket using this HTML page.
Server side code:
sockfd = socket(AF_INET, SOCK_STREAM, 0);
bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
listen(sockfd,5);
newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr,&clilen);
while (1)
[Code] .....
View 1 Replies
View Related
Sep 29, 2014
I am trying to concatenate two words from a file together. ex: "joe" "bob" into "joe bob". I have provided my function(s) below. I am somehow obtaining the terminal readout below. I have initialized my memory (I have to use dynamic, dont suggest fixing that). I have set up my char arrays (I HAVE TO USE CHAR ARRAYS (c-style string) DONT SUGGEST STRINGS) I know this is a weird way to do this, but it is academic. I am currently stuck. My file will read in to my tempfName and templName and will concatenate correctly into my tempName, but I am unable to correctly get into my (*playerPtr).name.
/* this is my terminal readout
joe bob
<- nothing is put into (*playerPtr).name, why not?
joe bob joe bob
seg fault*/
/****************************************************************/
//This is here to show my struct/playerInit
[Code]....
View 2 Replies
View Related
Dec 29, 2013
lets say I have a char array with four elements but only one char is used, does it write four elements or just one?
View 3 Replies
View Related
Sep 14, 2013
I want to compare alphabetically two arrays. I try this:
char a[10] = "AABC";
char b[10] = "ZABC";
if(a > b) cout << a;
else cout << b;
But it always outputs a. How can I compare char arrays?
View 3 Replies
View Related
Jun 8, 2012
I'm trying to get template specializations working for char * as well as for char[]. I.e. in the following code the last test does not use the specialization and fails:
Code:
#include <string>
#include <iostream>
#include <cstring>
template<typename T1, typename T2>
bool compare(const T1& lhs, const T2& rhs) {
[Code] ....
View 4 Replies
View Related
Mar 21, 2013
how to correctly use realloc on an array of char arrays? Say I want to store strings in arrays (=array of char arrays) and double the size of max. strings (y value):
Code:
int x=200;
int y=10;
char *carray[y];
for (int j = 0; j < y; ++j)
carray [j] = malloc (sizeof(char)*x);}
}
[code]...
fix the realloc part of my code?
View 2 Replies
View Related
Mar 30, 2013
I have most of the code working properly, but I'm having trouble with a certain area. Currently I have multiple 2D arrays. One is a char array and the other is an int array. In the int array I have to find the max number in each column, which I've done. The problem is, I need to print the max number's row in relation to the char array's row.
For example,
Code: int array[2][3] = {60 50 30 0 100 1}
The max numbers are 60, 100, 30.
char array[2][length+1] = {nameOne nameTwo}
How it needs to print:
nameOne has max score of 60.
nameTwo has max score of 100.
nameOne has max score of 30.
I just can't understand how to compare the two arrays in the right way, so it'll know that nameOne is associated with the numbers in row 0 and nameTwo in row 1, etc.
View 1 Replies
View Related
May 29, 2014
I've been experimenting with char arrays and getting user input through different methods.
int main() {
char userInput[21];
/*I understand that over here, a maximum of 20 letters can be input, and only letters before a space will be stored in userInput*/
std::cin >> userInput;
std::cout << userInput << std::endl;
[Code] ....
As I was testing, whenever I would input a single word for userInput (for example "hi"), the program would work as expected: it would output "hi" and I'd be able to input a sentence of sorts for userInput2 (for example "hello world") and have it outputted.
But if I were to input more than one word for user Input (for example "hi how are you"), the program would output "hi" as expected, but it wouldn't let me input anything for userInput2 and would just output the rest of the first input; in this case, "how are you" would be outputted and the program would end. I am not aware of the logic error at play.
View 7 Replies
View Related
Nov 16, 2013
I'm having trouble with passing a character array between functions of the same class. I have a function, buildGraph, that calls function getNextLine. The getNextLine essentially just retrieves the next line of an input file and stores it into a "char line[80]". However when I try to use "line" in my buildGraph function, it has nothing in it.
Here's my code:
Class
#define NUMNODES 10
using namespace std;
#pragma once
class Prog3Graph
[Code] ....
View 4 Replies
View Related
Apr 6, 2014
This program works as expected:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
unsigned long long int address;
float current;
unsigned char pressure_units;
}
[code]....
But I don't like how I had to use malloc and free. Is there a different way to accomplish copying the string into a char pointer without resorting to dynamic memory allocation?
View 1 Replies
View Related
May 29, 2014
I have make a code for simple chatting for single client but i want to make it for multiple client. what i need to modify in the code?
server.c
int main(int argc, char *argv[]) {
struct sockaddr_in localaddr,remoteaddr;
SOCKET s,ns;
char send_buffer[80],receive_buffer[80];
int n,bytes,addrlen;
memset(&localaddr,0,sizeof(localaddr));
[Code] .....
View 13 Replies
View Related
May 11, 2013
I want to send data from a file via winsock. The problem is that the only first line in the file is send.
Client
#include <iostream>
#include <winsock2.h>
#include <fstream>
#include <string>
#pragma comment(lib,"ws2_32.lib")
#define SERVER "127.0.0.1" //ip address of udp server
[Code] ....
View 2 Replies
View Related
Mar 13, 2015
I am currently trying to send a x264 nal unit using WINSOCK with a reliable multicast socket. It isn't decoding properly, and my initial thought is I am not receiving all the bytes correctly. I was hoping some fresh eyes can provide insight on errors or any improvements. I have seen some topics about this subject, and they showed sending entire structs with the socket. However, I am concerned about endianess so I am trying to stay away from that approach. I have commented out the decoding part, until I am confident that I am receiving the nal unit properly.
Server:
#include <WinSock2.h>
#include <WS2tcpip.h>
#include "wsrm.h"
#include <Windows.h>
#include <stdio.h>
#include <stdint.h>
#include <iostream>
#include "x264Encoder.h"
View 4 Replies
View Related
Jul 24, 2013
Code:
#include <algorithm>
#include <winsock.h>
void some_func() {
int result = std::min (0, 16384);
}
Okay, but the above code consistently gives me error C2589: '(' : illegal token on right side of '::'. I'm building with VC8. If I don't #include <winsock.h> the code compiles correctly but it doesn't matter if I #include <winsock.h> before or after including <algorithm>
I guess it might be due to one of my #defines but I've tried the code in a very minimal program and I still get the error.
View 7 Replies
View Related
Oct 31, 2013
Client:
Code: ....
string cmd = "dir c:filequeue > ";
string outputFilePath;
outputFilePath.append(getTempPath());
outputFilePath.append(" est.txt");
cmd.append(outputFilePath);
system(cmd.c_str()); // dir c:filequeue > %temp% est.txt
string content = get_file_contents(outputFilePath.c_str());
send(s, content.c_str(), content.length(), 0); I'm executing the "dir" command to get a listing of Folders/files of one Folder. Then I read the Output of the file and send it over winsock to the Server.
Now, the Problem is, I don't know how I can handle the recv properly, cause I have to set the buffer size without knowing, how much data is actually transfered. Sometimes maybe no files are in c:filequeue, sometimes a 100k.
So I tried to make recv as a Loop:
Server:
Code: ...
while (rc != SOCKET_ERROR) {
printf("
#");
gets(buf); //please no discussion about gets, I will Change this later ;)
if (strcmp(buf, "ls") == 0){
send(connectedSocket, "LIST", 4, 0);
[Code] .....
now it works, but as the recv blocks, it will never leave the Loop, even when the Transfer is finished.What should I do?
I believe I could make unblocking sockets, but that's a bit complicated. Isn't there an easier solution, with malloc'ing the buffer or a Signal when to leave the recv Loop?
View 3 Replies
View Related
Apr 16, 2015
This question is kind of a continuation of Winsock Sending X264 Nal Unit. I was finally able to solve that problem, turns out in the end I was missing a memcpy(). Now my current issue is, when I try to decode the received packets, I am shown a bunch of errors. I have attached a picture with my decoding errors. So since I am using a reliable multicast socket, which doesn't guarantee order of delivery. My initial thought is that I am getting packets out of order.
So my question is, how would I attach a packet header to my current data? Could I get away with sending the header separately? Similar to what I am doing with the nal length. If I do attach my header to the packet data, what is a good way to delimit the two?
Server:
#include <WinSock2.h>
#include <WS2tcpip.h>
#include "wsrm.h"
#include <Windows.h>
#include <stdio.h>
#include <stdint.h>
#include <iostream>
#include "x264Encoder.h"
[Code] ....
My client is just reading packets into a vector, because decoding it right away was too slow. So I thought that might have been an issue, so I decided to read in 200 packets just for testing purposes.
Attached image(s)
View 6 Replies
View Related
Jun 16, 2013
My problem is that I've created a Client and Server program in which they communicate. The Client is an SDL Application that allows you to play as a movable character IF YOU ARE CONNECTED TO THE SERVER. If not, You're unable to play. THIS WORKS!
However, Only one client is able to play on my server?? Anyone elses' window freezes and they are not allowed to play (As if not connected to the server). Here is my server code.
Code:
#include <iostream>
#include <winsock2.h>
#include <vector>
#include <process.h>
#include "Included/pthread.h"
#define MAX_THREADS 5
bool gamerunning = true;
[Code] ....
I'm having troubles allowing more than one client to play on the server.
View 2 Replies
View Related
Dec 18, 2014
I am trying to test my client-server socket program wherein the client connects to the server,client sends a message, server receives it and echo back to the client.So far, in the program server receives the message from the client, prints it BUT when it tries to send the message back to the client it shows an error.
sendto(): Invalid argument.I am new to socket programming.
Code:
//Server
#include<stdio.h> //printf
#include<string.h> //memset
#include<stdlib.h> //exit(0)
#include<netinet/in.h>
#include<sys/socket.h>
#define BUFLEN 512 //Max length of buffer
#define PORT 10003 //The port on which to listen for incoming data
}
[code]....
View 3 Replies
View Related