0
我必須實現一個java服務器,該服務器從智能手機上運行的ios應用程序接收數據(字符串)。在Java服務器上接收ios套接字數據
不幸的是,我從來沒有在類似的情況下工作,所以我想知道如果有人可以給我一些指針從哪裏開始(或信息資源來看看)。
我這裏有用來創建套接字和發送數據的IOS代碼:
-(bool)createSocket {
sock = 0;
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
NSLog(@"Failed to create socket, error=%s", strerror(errno));
return false;}
else {
return true;
NSLog(@"Socket created");
}
}
-(bool) send:(NSString*) msg ipAddress:(NSString*) ip port:(int) p
{
struct sockaddr_in destination;
unsigned int echolen;
int broadcast = 1;
if (socketCreated==false) {
NSLog(@"Socket assente");
return false;
}
else
{
/* Construct the server sockaddr_in structure */
memset(&destination, 0, sizeof(destination));
/* Clear struct */
destination.sin_family = AF_INET;
/* Internet/IP */
destination.sin_addr.s_addr = inet_addr([ip UTF8String]);
/* IP address */
destination.sin_port = htons(p);
/* server port */
setsockopt(sock,
IPPROTO_IP,
IP_MULTICAST_IF,
&destination,
sizeof(destination));
char *cmsg = [msg UTF8String];
echolen = strlen(cmsg);
// this call is what allows broadcast packets to be sent:
if (setsockopt(sock,
SOL_SOCKET,
SO_BROADCAST,
&broadcast,
sizeof broadcast) == -1)
{
perror("setsockopt (SO_BROADCAST)");
exit(1);
}
if (sendto(sock,
cmsg,
echolen,
0,
(struct sockaddr *) &destination,
sizeof(destination)) != echolen)
{
printf("Mismatch in number of sent bytes\n");
return false;
}
else
{
NSLog([NSString stringWithFormat:@"-> Tx: %@",msg]);
return true;
}
}
}
在此先感謝
謝謝你,我會立即調查它 – 2013-03-12 13:58:46