Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UDP server is not working with me #6

Open
ghost opened this issue Apr 26, 2015 · 6 comments
Open

UDP server is not working with me #6

ghost opened this issue Apr 26, 2015 · 6 comments

Comments

@ghost
Copy link

ghost commented Apr 26, 2015

Thank you very much for great effort.

I connect to Java server. server receive string message correctly.

when server reply to me with string message, callback is called but data,remoteip,remoteport are nil

I use this code :
func testudpserver(){
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), { () -> Void in
var server:UDPServer=UDPServer(addr:"127.0.0.1",port:19876)
var run:Bool=true
while run{
var (data,remoteip,remoteport)=server.recv(1024)
println("recive")
if let d=data{
if let str=String(bytes: d, encoding: NSUTF8StringEncoding){
println(str)
}
}
println(remoteip)
server.close()
break
}
})
}

@pengyunchou
Copy link
Member

@hishambakr
do you use this library as client or server?
UDPClient(swift) --> connect--> java server
or java client -->connect--> UDPClient(swift) ?

@ghost
Copy link
Author

ghost commented May 5, 2015

Thanks for reply,
I use library as server
java client -->connect--> UDPClient(swift)

I receive message but null.

I switched now to another library called "CocoaAsyncSocket" it is objective c but I used in in Swift and now I receive string message correctly

@pengyunchou
Copy link
Member

@hishambakr thanks for your feedback, i will check it.

@yamiyukiharu
Copy link

Hi, i seemed to found a line of code that prevents UdpServer from working properly, in yudpsocket.c line 60:
memset( &serv_addr, '\0', sizeof(serv_addr));
here you are wiping out all the data in ser_addr, which makes the socket bind to an address of 0.0.0.0
UdpServer is working fine for me once i removed that line.
Thanks a lot for this library!

@iain17
Copy link

iain17 commented Mar 26, 2016

Yeah got the same problem here. The tests even written (testudpserver, testudpclient) don't work.

@yamiyukiharu Looked that up and made a few adjustments to the "yudpsocket_server" function that made it work:
`
int yudpsocket_server(const char *addr,int port){
//create socket
int socketfd=socket(AF_INET, SOCK_DGRAM, 0);
int reuseon = 1;
int r = -1;
//bind
struct sockaddr_in serv_addr;
serv_addr.sin_len = sizeof(struct sockaddr_in);
serv_addr.sin_family = AF_INET;

if(addr == NULL ||
   strlen(addr) == 0 ||
   strcmp(addr, "loopback") == 0 ||
   strcmp(addr, "127.0.0.1") == 0 ||
   strcmp(addr, "localhost") == 0 ||
   strcmp(addr, "255.255.255.255") == 0)
{
    r = setsockopt( socketfd, SOL_SOCKET, SO_BROADCAST, &reuseon, sizeof(reuseon) );
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
}else{
    r = setsockopt( socketfd, SOL_SOCKET, SO_REUSEADDR, &reuseon, sizeof(reuseon) );
    serv_addr.sin_addr.s_addr = inet_addr(addr);
    memset( &serv_addr, '\0', sizeof(serv_addr));
}
serv_addr.sin_port = htons(port);

if(r==-1){
   return -1;
}
r=bind(socketfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
if(r==0){
    return socketfd;
}else{
    return -1;
}

}
`

@segabor
Copy link

segabor commented May 4, 2016

I can confirm the fix above works. @iain17 thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants