为什么我不能在客户端socket上接收数据。解决方法方法c#? [英] Why I cant Recieve Data on Client Side socket.recieve method method c# ?

查看:71
本文介绍了为什么我不能在客户端socket上接收数据。解决方法方法c#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从服务器向客户端发送/接收文本。



我的服务器端代码就是这个。它的node.js,socket.io代码。在服务器端我收到客户端的数据..这部分工作对我来说很好..

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;

函数处理程序(req,res){

fs.readFile(__ dirname +'/ index.html',

函数(错误,数据){

if(err){

res.writeHead(500);

返回res.end('错误加载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);

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上面的代码,你可以看到,我发送嘿,使用这个方法。



In Above Code you can see,i am sending "Hey" ,,using this mothod.

Quote:

socket.send('hi');

socket.send('hi');





这个文本我想在我的客户端接收..我在客户端用C#写这个方法..





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]; //文本的长度Hello world!

//接收超时数据10s





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

{



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

}

socket.Close();



}

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();

}





我没有收到任何文字。你可以帮我吗。

我能够从客户端c#向服务器发送数据。只能在客户端接收文本..



谢谢提前。



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 .

推荐答案

你应该打电话给

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



否则套接字未连接......


Otherwise the socket is not connected...


这篇关于为什么我不能在客户端socket上接收数据。解决方法方法c#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆