C++ :: How To Multithread Socket Serve

Feb 19, 2013

I'm having a bit of trouble learning how to multithread a socket server. I have everything working perfectly fine, but it only accepts one client (I would like multiple clients). some sort of example (I have troubles understanding some things, unless its visual).

View 4 Replies


ADVERTISEMENT

C++ :: How To Send Email Using Google SMTP Serve

Jan 2, 2014

I want to send an email using Google's SMTP server. How do I achieve it?

View 2 Replies View Related

C# :: How To Multithread Port Scanning App

Jan 24, 2012

I'm creating a small port scanning app which use a port range defined by user.My problem with the code is that I find it not efficient enough meaning that it ping very slow, especially when ports are not responding meaning that I must wait for the timeout before continuing to next port.

I want to make this small app threaded. The problem here is that I'm kind of blank of how to implement threading in my program. This is my code which is a method which does the actual checking, the other part of the code is a simple button with for loop for advancing my port number.

Code:

private void ScanPort(IPAddress address, int port) {
using (TcpClient client = new TcpClient() {
IAsyncResult result = client.BeginConnect(address, port, null, null);

if (result.AsyncWaitHandle.WaitOne((int)nudTimeout.Value, false)) txtDisplay.AppendText("Port: " + port + " is open." + Environment.NewLine);
else txtDisplay.AppendText("Port: " + port + " is closed." + Environment.NewLine);
} }

I have read some basic threading tutorial but I just don't know how to implement this piece of code so I can check ports much faster.

View 3 Replies View Related

C :: Writing Log File In Multithread Program

Oct 20, 2014

I'm currently finishing writing some small application. I want to be able to log important information about the program execution to a logfile, and I have several questions.

First of all - I'd prefer to make the part that logs information to a file separate from the code I've already written. So, what interface should I expose to the rest of the program? Is one function void log(const char*); enough?

Another thing that came to my mind; my program runs two threads, and I want to be able to write to the log file from both threads, so the question is: Should I make sure that the writing to the file is mutually exclusive?

And if so, whose responsibility is it to make the logging to the file thread-safe? The actual part that does the logging (void log(const char*) for that matter), or the parts of the program that calls log(const char*) ?

And lastly, and probably less importantly, where is it customary to save the logfile? (the user's home folder maybe?)

View 3 Replies View Related

C++ :: Finding Multithread Program Error?

Aug 3, 2013

How do I find an error in a multithreading program (written in C)? I get an address, but when I put it into psp-addr2line, i get

??
??:0

View 2 Replies View Related

C# :: MultiThread Blocking Updates During Read

Oct 6, 2014

I have a class holding my data strcuture:

// Holding real time option data - should be thread safe
public class OptionRTHolder {
ConcurrentDictionary<double, Quates> book_calls;
ConcurrentDictionary<double, Quates> book_puts;
ConcurrentDictionary<double, List<Single_qoute>> transaction_calls;
ConcurrentDictionary<double, List<Single_qoute>> transaction_puts;
ConcurrentDictionary<double, int> volume_calls;
ConcurrentDictionary<double, int> volume_puts;
DateTime curr_time;
}

I get a stream of data messages, that come in order, each message carries a time and data. I have many threads in paralell pulling messages and in turn update the data strucutre as follows:

Quates and volume data are just overrun (new value overrun former values), also thier size and meta data is fixed (they don't grow post init).

Via the list I have a problem, I would want to lock only the list I am working on. so other lists can update in paralell, what I had in mind was to warp the list in order to warp the add/get calls to make it thread safety, this I think will achieve this goal and I would like to get some assertion from this forum if you may.

Here come my real problem, there are points in time I want to read/copy this structure, during this time I want to block all updating threads as I want to freeze structure in time, is that achiveable ? Idea that I had is to use something like aquire that will get a function (update/read) and a flag that will cause a lock when the read is on...something like that.

View 3 Replies View Related

C++ :: Multithread Application - Global Dynamic Array

Nov 18, 2013

Let's have global dynamic array p defined as

volatile int p[];

Let's use it in a multithread application, where every thread can change array's size by

new_p = realloc(p, new_size);
if (!new_p) exit 1;
p = new_p;

For simplify assume that we have no problems with it's size and indexing.

Expression like

int x = p[i];

is unsafe cause thread (1) takes the pointer and (2) add the index. Executing of this thread may be stoped after first step before second step. In this time other thread can realloc the array, which move it to some other place in memory. Executing second step after it will add index to wrong pointer. So we will have segmentation fault.

View 2 Replies View Related

C :: UDO Socket Programming

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

C++ :: UDP Socket Library On Linux

Jan 29, 2014

I'm attempting to write a little UDP socket library in c++ on linux so a user can just create a new instance of a UDPSocket class, specify destination ip and port, and just connect. Then the user should be able to call send() or receive() in any order they want.. and here I encounter a little problem..

Most of the tutorials for udp socket sending out there include a bind() call when you create your "server" that is supposed to receive data, but the code that send data does not need one. Because I also want my library to support unicast/broadcast/multicast, I have read that I need to set the socket option SO_REUSEADDR on my sockets (since multiple sockets will need to be connected to same destination IP/port for broadcast/multicast)

My question is.. do I need to create 2 socket handles per "UDPSocke in order to make this work? One for sending and one for receiving data? In my code when I try to work with only 1 socket, it is only able to receive stuff from itself on unicast.. Or should I just remove the SO_REUSEADDR when in unicast mode, then try to bind with both sockets, accept that the bind will only work on the 1st socket, and take it from there?

View 1 Replies View Related

C :: Sending Matrix Over UDP Socket

May 18, 2014

I want to apply program to send matrix from client and server ,and return back a processed matrix ..

How I can do it using UDP ,it was easy to use tcp for that!?

View 3 Replies View Related

C :: Sending Integer Via Socket

Dec 18, 2014

Verify my Method . I am not getting expected output ..

Code:

int Amount=500;
send(newSocket,Amount,4,0);
close(welcomeSocket);
recv(clientSocket,Amount,4,0);
// Amount= ntohl(Amount);
printf("Data received: %d",atoi(Amount[0]));

View 5 Replies View Related

C :: Socket Programming Getting Segfault

May 22, 2013

I am trying to send a packet across a client/server communication. I am getting a seg fault (while running the program(It compiles fine)) when I try to read the neighbor file, You should be able to see this below the comment /***** Read neighbor file***/ :

Code:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
}

[code]....

View 2 Replies View Related

C# :: Tcp Socket Buffer Size?

Dec 17, 2014

get some opinions on, or established methods on how your buffer size should be decided.

View 2 Replies View Related

C++ :: Struct Of Ints Via Socket?

Feb 7, 2014

I have some paper work to do about a game I should be able to play with my class mates. We should be able to send and recive a struct concerning about the 'moves' of the pieces at the game. This struct is made of 4 ints, simple as that.

I had a previous paper work to do where I'd have to send ints and that was ok, but now that I have to send a struct I'm failing to do so. This is What I've tried:

//SERVER
struct message{
int code;
int piece;
int x;
int y;
}send;
bytes_sent=write(new_socket,&send,sizeof(message));
cout<<bytes_sent;

So far so good, I see 16 as bytes_sent's value.

On the other hand:

//CLIENT
struct message{
int code;
int piece;
int x;
int y;
}recive;
bytes_recvd=read(my_socket,&recive,sizeof(message));
cout<<bytes_recvd;

I always see 0 as the bytes_recvd's value.

View 3 Replies View Related

C# :: Socket TCP Send / Receive

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

C/C++ :: How To Send Many Images Through Socket

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

C :: Socket Programming And Console Control

Jan 4, 2014

I would like to make a program for final project, which can let me send any file to my computer at home, and i can access any files in my computer when i am not at home.sending and getting file will be with socket programming.

i would like to reach all folders in computer, not only the folder, where client program exist. how can i access other folders at remote computer by using my program?

View 4 Replies View Related

C++ :: HTTP Response Receive Using Socket

May 18, 2014

I make a simple c++ http post request using socket and it is working fine. But how to get only the message of the response. Here is my receive code:

char buffer[1028];
recv(socket, buffer, sizeof(buffer), 0);
printf("%s", buffer);

Then here is the response that the http send back.

HTTP/1.1 200 OK
Data: Mon, 19 May 2014 12:46:36 GMT
Server: Apache/2.4.9 (Win32) OpenSSL/0.9.8y PHP/5.4.27
X-Powered-By: PHP/5.4.27
Coneten-Length: 28
Content-Type: text/html

All done! Do some stuff now. So what I want to try is get 3 things in this response and put them to string vector but how to do this. What I need is to parse the response and the 200 OK, 28 and All done! Do some stuff now. then put them to string vector.

expected result

response[0] = 200 OK;
response[1] = 28
response[2] = All done! Do some stuff now

View 2 Replies View Related

C# :: PictureBox To Memory Stream In Socket?

Mar 7, 2014

I Was Try To Change PictureBox To The Memory Stream And Then I Read Memory Stream Bytes And I Send Over The Netwrok Here Is Code

MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); // we save the image in side the Memory strem
byte[] pic_array = new byte[ms.Length];
ms.Position = 0;
ms.Read(pic_array, 0, pic_array.Length); //

[Code]...

but when i want get the Data o mean Bytes from Network tream How can i save in memory stream then dispaly in the picture box ? i try some thing but it is gave me error !

Socket handlerSocket = (Socket)alSockets[alSockets.Count - 1];
NetworkStream networkStream = new
NetworkStream(handlerSocket);
int thisRead = 0;
int blockSize = 1024;

[Code]...

View 2 Replies View Related

C/C++ :: Set Timeout On A Socket Using Select Function

Mar 24, 2014

As requested by an exercise I have to write a function which:

- takes as parameter 1 unsigned which represent a timout value, 1 string which represent an IP address and an array containing port's numbers
- sends an UDP packet (1024 byte with NULL content) to each pair <IP,port>
- waits for the first reply, if it receives a packet within the timeout (set by select() function), the function returns the port of the process who replied, else it returns -1

I think I can do anything of above, but I've not understood how to set and use the select() in this case.

This is my code as it is at the moment

#define BUFLEN 1024
int comUDP ( unsigned timeout, char* ip, int ports[] ) {
int s_ds_sock, c_ds_sock;
struct sockaddr_in server_socket, client_socket;
char msg[BUFLEN] = NULL; //message to send to the clients
fd_set fds;

[Code] .....

View 2 Replies View Related

C :: Implementing Non Blocking UDP Socket By Select Function

Apr 22, 2013

I wanted to create an asynchronous/non-blocking udp client server application where the client and server were supposed to chat away with each other without waiting for each other's turns. I came to know that this could be done by select()... here is my Server(Mentioning only the communication part):

Code:
fd_set readfds,writefds;
while(1){
FD_ZERO(&readfds);
FD_ZERO(&writefds);
FD_SET(sd,&readfds);
FD_SET(sd,&writefds);

int rv = select(n, &readfds, NULL, NULL, NULL);

[Code] .....

At first on the server side I wrote:
int rv = select(n, &readfds, &writefds, NULL, NULL);

But that led to the printing of an entire empty array on the server console when the server initialised in addition to the fact that communication between the server and client was not proper. removing "&writefds" did remove the redundant data but the improper communication issue still persists...

View 3 Replies View Related

C :: Program Doesn't Print Data From Socket

May 7, 2014

I created a basic socket server, which listensing for incoming udp data. When I run the netcat program, I get a response:

Code:
$ nc -luv 1732
Connection from 10.50.11.12 port 1732 [udp/*] accepted
(?@??8?? ??.?n?5
(?@??8?? ??.?n?5|?>)
(?@??8?? ??.?n?5|?>)
^C |?>)

But with my c program it doesn't give the response. It should say something like "here is the message: " and then give a message.

Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>

[Code] .....

View 2 Replies View Related

C++ :: Streambuf For Socket - Send / Receive Data

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

C++ :: Command Line And Waiting For A Connection (Socket)

Feb 7, 2014

I am designing a server program in C++ and now that everything works for the clients, I would like to implement a command line. The code waits for a connection with the predefined function "new_socket = accept(serversocket,(struct sockaddr *)&client,&addrsize);". I already made a function for the command line but I can't find a way (in my brain or on the web) to accept commands (getline) whilst no connection is accepted yet. (I mean wait for a connection. If no connection, then be ready to receive some arguments from the command line). Here is part of the code:

cout << "Waiting for connection/command...";
command_line_function(); //getline(cin,string)
while(true){
new_socket = accept(serversocket,(struct sockaddr *)&client,&addrsize);
if(new_socket == INVALID_SOCKET){ //INVALID_SOCKET is an error somehow.
error_function();}
cout << "ACCEPTED
";
//etc.
}

View 5 Replies View Related

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 View Related

C++ :: Using Same Socket For Second Send / Receive Giving No Result?

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







Copyrights 2005-15 www.BigResource.com, All rights reserved