I am trying to set up a Multihop AdHoc 802.11g Network in ns-3.
To get started I used the example 'wifi-simple-adhoc-grid.cc'.
The example uses UDP, but I want to use TCP. Therefore I switched
TypeId tid = ns3::UdpSocketFactory::GetTypeId();
Ptr<Socket> recvSink = Socket::CreateSocket (c.Get (sinkNode), tid);
InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 80);
recvSink->Bind (local);
recvSink->SetRecvCallback (MakeCallback (&ReceivePacket));
to
TypeId tid = ns3::TcpSocketFactory::GetTypeId();
Ptr<Socket> recvSink = Socket::CreateSocket (c.Get (sinkNode), tid);
InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 80);
recvSink->Bind (local);
recvSink->SetRecvCallback (MakeCallback (&ReceivePacket));
Ptr<Socket> source = Socket::CreateSocket (c.Get (sourceNode), tid);
InetSocketAddress remote = InetSocketAddress (i.GetAddress (sinkNode, 0), 80);
Sending Packets is no Problem, but ReceivePacket is never called, which means, that socket Sink receives no packets.
The whole code: https://gist.github.com/3023757
Routes output by:
Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper>
("wifi-multihop.routes", std::ios::out)
You can call SetRevCallback function your SetAcceptCallback function.
Explicitly,
recvSink->SetAcceptCallback (MakeNullCallback<bool, Ptr<Socket>,const Address &> (),MakeCallback(&accept));
In the accept function
void accept(Ptr<Socket> socket,const ns3::Address& from)
{
std::cout<<"Connection accepted"<< std::endl;
socket->SetRecvCallback (MakeCallback (&ReceivePacket));
}
Related
I am running the example sketches for a server and client:
Client:
/**
* Shared Drawing Canvas (Client)
* by Alexander R. Galloway.
*
* The Processing Client class is instantiated by specifying a remote
* address and port number to which the socket connection should be made.
* Once the connection is made, the client may read (or write) data to the server.
* Before running this program, start the Shared Drawing Canvas (Server) program.
*/
import processing.net.*;
Client c;
String input;
int data[];
void setup()
{
size(450, 255);
background(204);
stroke(0);
frameRate(5); // Slow it down a little
// Connect to the server's IP address and port
c = new Client(this, "127.0.0.1", 8080); // Replace with your server's IP and port
}
void draw()
{
if (mousePressed == true) {
// Draw our line
stroke(255);
line(pmouseX, pmouseY, mouseX, mouseY);
// Send mouse coords to other person
c.write(pmouseX + " " + pmouseY + " " + mouseX + " " + mouseY + "\n");
}
// Receive data from server
if (c.available() > 0) {
input = c.readString();
input = input.substring(0, input.indexOf("\n")); // Only up to the newline
data = int(split(input, ' ')); // Split values into an array
// Draw line using received coords
stroke(0);
line(data[0], data[1], data[2], data[3]);
}
}
Server:
/**
* Shared Drawing Canvas (Server)
* by Alexander R. Galloway.
*
* A server that shares a drawing canvas between two computers.
* In order to open a socket connection, a server must select a
* port on which to listen for incoming clients and through which
* to communicate. Once the socket is established, a client may
* connect to the server and send or receive commands and data.
* Get this program running and then start the Shared Drawing
* Canvas (Client) program so see how they interact.
*/
import processing.net.*;
Server s;
Client c;
String input;
int data[];
void setup()
{
size(450, 255);
background(204);
stroke(0);
frameRate(5); // Slow it down a little
s = new Server(this, 8080); // Start a simple server on a port
}
void draw()
{
if (mousePressed == true) {
// Draw our line
stroke(255);
line(pmouseX, pmouseY, mouseX, mouseY);
// Send mouse coords to other person
s.write(pmouseX + " " + pmouseY + " " + mouseX + " " + mouseY + "\n");
}
// Receive data from client
c = s.available();
if (c != null) {
input = c.readString();
input = input.substring(0, input.indexOf("\n")); // Only up to the newline
data = int(split(input, ' ')); // Split values into an array
// Draw line using received coords
stroke(0);
line(data[0], data[1], data[2], data[3]);
}
}
Now I think I should be able to inspect what the server is doing when I go to http://127.0.0.1:8080/. Unfortunately my browser seems to be unable to establish a connection with the server. Have I done something wrong and can it be fixed or are my expectations unrealistic?
The server and the client communicate via a network socket. They pass data in a format that is not understandable for your browser. The way to get the example to run is to launch the server first and the client second (you need to start the Processing environment two times). Now you should be able to draw lines on one side and see them being copied on the other side.
If you want to inspect what the server and client are doing, you can add println statements to the code.
The example code that you are using can be found here on GitHub:
https://github.com/processing/processing/tree/master/java/libraries/net/examples
I want to create a simple server using GameMaker 1.4. But it's function never works on the first try, no idea why..
var networkType = network_socket_tcp;
var port = 50000;
var maxClients = 32;
var bufferSize = 1024;
var bufferType = buffer_fixed;
var bufferAlign = 1;
server = network_create_server(networkType, port, maxClients);
global.buffer = buffer_create(bufferSize,bufferType,bufferAlign);
sockets = ds_list_create();
if server s_msg("Server is up! Port: " + string(port));
else s_msg("Server is down!");
The message will be "Server down". No matter the port neither the net type.
When I try the same code with a loop, it works on the second try.
Like this:
var networkType = network_socket_tcp;
var port = 50000;
var maxClients = 32;
var bufferSize = 1024;
var bufferType = buffer_fixed;
var bufferAlign = 1;
server = network_create_server(networkType, port, maxClients);
//////////////////////////////////////////////////////
while (!server && port < 65535){
port++;
server = network_create_server(networkType, port, maxClients);
}
//////////////////////////////////////////////////////
global.buffer = buffer_create(bufferSize,bufferType,bufferAlign);
sockets = ds_list_create();
if server s_msg("Server is up! Port: " + string(port));
else s_msg("Server is down!");
Here the message will result on "Server up! Port: 50001".
As I said, no matter the port.. it could be set to 50001 at the beginning resulting on create the server to 50002.
Could someone point my mistake?
SOLUTION (From user Humayun in GM Forums): network_create_server returns an index from 0, so it can't be checked as false/true since the first index would be 0 which returns false. Explains that on the second try i was creating a second instance to the sever variable indexed as 1, and so returning true.
Recently, I started playing around with Game Maker to make a very simple game with the ability to play online (multiplayer). I made a very simple client & server. I can send data from the server to the client, but I can't send the data from the client to the server.
Client: (Create event)
client_socket = network_create_socket(network_socket_tcp);
var server = network_connect(client_socket, "127.0.0.1", 5200);
if(server < 0) show_message("Could not connect! Try turning on the server?");
else{ //Send string
var t_buffer = buffer_create(256, buffer_grow, 1);
buffer_seek(t_buffer, buffer_seek_start, 0);
buffer_write(t_buffer , buffer_string, "Hello");
network_send_packet(client_socket, t_buffer, buffer_tell(t_buffer));
buffer_delete(t_buffer);
}
Server: (Create event)
server_socket = network_create_server(network_socket_tcp, 5200, 5);
(Async Network event)
var n_id = ds_map_find_value(async_load, "id");
if(n_id == server_socket){
var t = ds_map_find_value(async_load, "type");
socketlist = ds_list_create();
if(t == network_type_connect){
var sock = ds_map_find_value(async_load, "socket");
ds_list_add(socketlist, sock);
}
if(t == network_type_data){
var t_buffer = ds_map_find_value(async_load, "buffer");
var cmd_type = buffer_read(t_buffer, buffer_string);
show_message(cmd_type);
}
//show_message("Something happened!");
}
For some reason, the async network event in the server is never triggered when the client sends data. The message Something happened! only comes up when the client either connects or disconnects, but not when data is sent. Using almost the exact same code, I can send from the server, but not vice versa. Is this a problem with the code or just a server/client limitation?
This is the server side code:
var n_id = ds_map_find_value(async_load, "id");
if(n_id == server_socket){
var t = ds_map_find_value(async_load, "type");
socketlist = ds_list_create();
if(t == network_type_connect){
sock = ds_map_find_value(async_load, "socket");
ds_list_add(socketlist, sock);
}
}
if(n_id == sock) {
var t_buffer = ds_map_find_value(async_load, "buffer");
var cmd_type = buffer_read(t_buffer, buffer_string);
show_message(cmd_type);
}
You have to use the socket id when a message is arriving. The network_type_data will never be triggered.
Also, you have to declare the sock variable in the server's create event with a negative number (like noone (-4)).
I've been trying to modify the tcp server example with LwIP in STM32F4DISCOVERY board. I have to write a sender which does not necessarily have to reply server responses. It can send data with 100 ms frequency, for example.
Firstly, the example of TCP server is like this:
static void tcpecho_thread(void *arg)
{
struct netconn *conn, *newconn;
err_t err;
LWIP_UNUSED_ARG(arg);
/* Create a new connection identifier. */
conn = netconn_new(NETCONN_TCP);
if (conn!=NULL) {
/* Bind connection to well known port number 7. */
err = netconn_bind(conn, NULL, DEST_PORT);
if (err == ERR_OK) {
/* Tell connection to go into listening mode. */
netconn_listen(conn);
while (1) {
/* Grab new connection. */
newconn = netconn_accept(conn);
/* Process the new connection. */
if (newconn) {
struct netbuf *buf;
void *data;
u16_t len;
while ((buf = netconn_recv(newconn)) != NULL) {
do {
netbuf_data(buf, &data, &len);
//Incoming package
.....
//Check for data
if (DATA IS CORRECT)
{
//Reply
data = "OK";
len = 2;
netconn_write(newconn, data, len, NETCONN_COPY);
}
} while (netbuf_next(buf) >= 0);
netbuf_delete(buf);
}
/* Close connection and discard connection identifier. */
netconn_close(newconn);
netconn_delete(newconn);
}
}
} else {
printf(" can not bind TCP netconn");
}
} else {
printf("can not create TCP netconn");
}
}
I modified this code to obtain a client version, this is what I've got so far:
static void tcpecho_thread(void *arg)
{
struct netconn *xNetConn = NULL;
struct ip_addr local_ip;
struct ip_addr remote_ip;
int rc1, rc2;
struct netbuf *Gonderilen_Buf = NULL;
struct netbuf *gonderilen_buf = NULL;
void *b_data;
u16_t b_len;
IP4_ADDR( &local_ip, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3 );
IP4_ADDR( &remote_ip, DEST_IP_ADDR0, DEST_IP_ADDR1, DEST_IP_ADDR2, DEST_IP_ADDR3 );
xNetConn = netconn_new ( NETCONN_TCP );
rc1 = netconn_bind ( xNetConn, &local_ip, DEST_PORT );
rc2 = netconn_connect ( xNetConn, &remote_ip, DEST_PORT );
b_data = "+24C"; // Data to be send
b_len = sizeof ( b_data );
while(1)
{
if ( rc1 == ERR_OK )
{
// If button pressed, send data "+24C" to server
if (GPIO_ReadInputDataBit (GPIOA, GPIO_Pin_0) == Bit_SET)
{
Buf = netbuf_new();
netbuf_alloc(Buf, 4); // 4 bytes of buffer
Buf->p->payload = "+24C";
Buf->p->len = 4;
netconn_write(xNetConn, Buf->p->payload, b_len, NETCONN_COPY);
vTaskDelay(100); // To see the result easily in Comm Operator
netbuf_delete(Buf);
}
}
if ( rc1 != ERR_OK || rc2 != ERR_OK )
{
netconn_delete ( xNetConn );
}
}
}
While the writing operation works, netconn_write sends what's on its buffer. It doesnt care whether b_data is NULL or not. I've tested it by adding the line b_data = NULL;
So the resulting output in Comm Operator is like this:
Rec:(02:47:27)+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C
However, I want it to work like this:
Rec:(02:47:22)+24C
Rec:(02:47:27)+24C
Rec:(02:57:12)+24C
Rec:(02:58:41)+24C
The desired write operation happens when I wait for around 8 seconds before I push the button again.
Since netconn_write function does not allow writing to a buffer, I'm not able to clear it. And netconn_send is only allowed for UDP connections.
I need some guidance to understand the problem and to generate a solution for it.
Any help will be greately appreciated.
It's just a matter of printing the result in the correct way.
You can try to add this part of code before writing in the netbuf data structure:
char buffer[20];
sprintf(buffer,"24+ \n");
Buf->p->payload = "+24C";
I see one or two problems in your code, depending on what you want it exactly to do. First of all, you're not sending b_data at all, but a constant string:
b_data = "+24C"; // Data to be send
and then
Buf->p->payload = "+24C";
Buf->p->len = 4;
netconn_write(xNetConn, Buf->p->payload, b_len, NETCONN_COPY);
b_data is not anywhere mentioned there. What is sent is the payload. Try Buf->p->payload = b_data; if it's what you want to achieve.
Second, if you want the +24C text to be sent only once when you push the button, you'll have to have a loop to wait for the button to open again before continuing the loop, or it will send +24C continuously until you stop pushing the button. Something in this direction:
while (GPIO_ReadInputDataBit (GPIOA, GPIO_Pin_0) == Bit_SET) {
vTaskDelay(1);
}
Could anyone please provide me with the code or link to send and receive broadcast messages if possible using UDP?
I have been stuck in a problem and hope if u guys could help me resolve it.
Thanks
Here's a C# example:
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
class MainClass {
static void Main(string[] args)
{
ThreadPool.QueueUserWorkItem(StartUDPListener);
UdpClient udpClient = new UdpClient();
udpClient.Send(new byte[]{0x00}, 1, new IPEndPoint(IPAddress.Broadcast, 4567));
Console.ReadLine();
}
private static void StartUDPListener(object state) {
UdpClient udpServer = new UdpClient(new IPEndPoint(IPAddress.Broadcast, 4567));
IPEndPoint remoteEndPoint = null;
udpServer.Receive(ref remoteEndPoint);
Console.WriteLine("UDP broadcast received from " + remoteEndPoint + ".");
}
}
Here is example code for both the broadcast sender and receiver.
It should be easily portable to any language which has access to the standard Berkly Sockets API.
#!/usr/bin/perl -w
# broadcast sender script
use strict;
use diagnostics;
use Socket;
my $sock;
my $receiverPort = 9722;
my $senderPort = 9721;
socket($sock, PF_INET, SOCK_DGRAM, getprotobyname('udp')) || die "socket: $!";
setsockopt($sock, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) || die "setsockopt: $!";
setsockopt($sock, SOL_SOCKET, SO_BROADCAST, pack("l", 1)) or die "sockopt: $!";
bind($sock, sockaddr_in($senderPort, inet_aton('192.168.2.103'))) || die "bind: $!";
while (1) {
my $datastring = `date`;
my $bytes = send($sock, $datastring, 0,
sockaddr_in($receiverPort, inet_aton('192.168.2.255')));
if (!defined($bytes)) {
print("$!\n");
} else {
print("sent $bytes bytes\n");
}
sleep(2);
}
#!/usr/bin/perl -w
# broadcast receiver script
use strict;
use diagnostics;
use Socket;
my $sock;
socket($sock, PF_INET, SOCK_DGRAM, getprotobyname('udp')) || die "socket: $!";
setsockopt($sock, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) || die "setsockopt: $!";
bind($sock, sockaddr_in(9722, inet_aton('192.168.2.255'))) || die "bind: $!";
# just loop forever listening for packets
while (1) {
my $datastring = '';
my $hispaddr = recv($sock, $datastring, 64, 0); # blocking recv
if (!defined($hispaddr)) {
print("recv failed: $!\n");
next;
}
print "$datastring";
}
I'm not going to post code, just a couple of observations:
Sending a UDP broadcast is just like sending a unicast packet - only the destination address is different. This can be INADDR_BROADCAST (255.255.255.255) but that can cause problems on systems with multiple network interfaces. It's better to send to the specific broadcast address for the interface that you want to send on. The only significant caveat is that you may need to set the SO_BROADCAST socket option before your O/S will permit sending the broadcast.
Receiving a UDP broadcast is exactly like receiving a unicast packet. No special code is necessary, but you should have the receiver bound to INADDR_ANY.
I'm just starting to learn this, but this was my first working example, it might help you.
Receiver code:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class UDPReceiver
{
static void Main(string[] args)
{
IPEndPoint endPoint;
using (UdpClient client = new UdpClient(9998))
{
bool connected = true;
while (connected)
{
byte[] dataBytes = client.Receive(ref endPoint);
string dataString = Encoding.UTF8.GetString(dataBytes);
if (dataString.ToLower() != "exit")
Console.WriteLine(dataString);
else
connected = false;
}
}
}
}
Sender code:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class UDPSender
{
static void Main(string[] args)
{
using (UdpClient client = new UdpClient())
{
bool connected = true;
while (connected)
{
string dataString = Console.ReadLine();
byte[] dataBytes = Encoding.UTF8.GetBytes(dataString);
client.Send(dataBytes, dataBytes.Length, new IPEndPoint(IPAddress.Broadcast, 9998));
if (dataString.ToLower() == "exit")
connected = false;
}
}
}
}