Click here to Skip to main content
15,900,714 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I sending/Receiving text from Server to client.

my server side code is this. its node.js, socket.io code. on server side i receive data from client..this part work fine for me..
Quote:
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
app.listen(8070);
var mysocket = 0;
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
io.sockets.on('connection', function (socket) {
socket.send('hi');
console.log('index.html connected');
mysocket = socket;
});


//udp server on 41181
var dgram = require("dgram");
var server = dgram.createSocket("udp4");
server.on("message", function (msg, rinfo) {
console.log("msg: " + msg);
if (mysocket != 0){
mysocket.emit('field', ""+msg);
}
});
server.on("listening", function () {
var address = server.address();
console.log("udp server listening " + address.address + ":" + address.port);

});
server.bind(41181);


In Above Code you can see,i am sending "Hey" ,,using this mothod.
Quote:
socket.send('hi');


this Text I want Receive ON my Client Side.. I writing this Method in C# in my client side..

Quote:
public static void RecieveUDP(int count)
{
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.Connect("127.0.0.1", 41181);
byte[] bytes = new byte[256]; // length of the text "Hello world!"
// receive data with timeout 10s


for (int x = 0; x < count; x++)
{

Console.WriteLine(socket.Receive(bytes));
}
socket.Close();

}


I am not receiving any text . Can you help me Please.
I am able to send data from client side c# to Server .just can't receive text on client side..

Thanks Advance .
Posted
Updated 12-Dec-14 0:46am
v2

1 solution

You should call e.g.
C#
socket.Connect("127.0.0.1", 8888);

Otherwise the socket is not connected...
 
Share this answer
 
Comments
ShortProgrammer 12-Dec-14 6:26am    
yeah but i still can't see "Hi" on my Client Side. ,as i am sending "hey" from Server..
ShortProgrammer 12-Dec-14 6:46am    
Please check question Again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900