- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 LXsocket作为我的网络提供商我已经设置了服务器和客户端进行测试。我还安装了wireshark来查看发送的内容客户:
socket = [[LXSocket alloc] init];
if ([socket connect: host port: port] == NO)
{
//cant connect!
[socket release];
NSLog(@"cannot create socket");
return FALSE;
}
//connect hex string being sent
const char connectByteArray[] = {
0x01, 0x00, 0x07, 0x7f, 0x00, 0x00, 0x01, 0x00, 0x03, 0x40
};
//send bytes this works and the packets shown in wireshark reflect the byte array above
[socket sendBytes:connectByteArray length:sizeof(connectByteArray)];
//receive bytes
id *object = [socket readBytesWithLength:sizeof(connectByteArray)];
服务器:
LXSocket* serverSocket = [[LXSocket alloc] init];
[serverSocket bindToPort: 1234];
[serverSocket listen: 5];
LXSocket *socket = [serverSocket accept];
const char connectByteArray[] = {
0x01, 0x00, 0x07, 0x7f, 0x00, 0x00, 0x01, 0x00, 0x03, 0x40
};
[socket sendBytes:connectByteArray length:sizeof(connectByteArray)];
我的问题是我无法让 cocoa 正确导出字节数组。我尝试了许多不同的格式标签,但没有任何运气。我怎样才能让它记录正确的字节数组,即。 01:00:07:7f:00:00:01:00:03:40 ???
NSLog(@"return data %X",object);
NSLog(@"return data %f",object);
NSLog(@"return data %e",object);
NSLog(@"return data %llu",object);
NSLog(@"return data %hu",object);
2011-08-24 08:24:44.507 Chameleon[5574:207]返回数据4C053B0
2011-08-24 08:24:44.508 Chameleon[5574:207]返回数据0.000000
2011-08-24 08:24:44.509 Chameleon[5574:207]返回数据2.125934e-313
2011-08-24 08:24:44.510 Chameleon[5574:207]返回数据43029386160
2011-08-24 08:24:44.511 Chameleon[5574:207]返回数据21424
#import "LXSocket.h"
#import <sys/socket.h>
#import <sys/types.h>
#import <netinet/in.h>
#import <arpa/inet.h>
#import <netdb.h>
@implementation LXSocket
+ (id)socket {
return [[[LXSocket alloc] init] autorelease];
}
- (id)initWithCSocket:(SOCKET)_socket {
if ((self = [super init])) {
m_socket = _socket;
}
return self;
}
- (id)init {
if ((self = [super init])) {
m_socket = socket(AF_INET, SOCK_STREAM, 0);
}
return self;
}
- (BOOL)bindToPort:(unsigned)port {
struct sockaddr_in service;
service.sin_family = AF_INET;
service.sin_addr.s_addr = INADDR_ANY;
service.sin_port = htons(port);
if (bind(m_socket, (struct sockaddr *) &service, sizeof(service)) == -1) {
NSLog(@"bind() failed -- %s\n", strerror(errno));
close(m_socket);
return NO;
} else return YES;
}
- (BOOL)listen:(unsigned)limit {
if (listen(m_socket, limit) == -1) {
printf("listen(): Error listening on socket\n");
return NO;
} else return YES;
}
- (LXSocket*)accept {
SOCKET AcceptSocket;
while (YES) {
AcceptSocket = -1;
while (AcceptSocket == -1) {
AcceptSocket = accept(m_socket, NULL, NULL);
}
//printf("Server: Client Connected!\n");
//m_socket = AcceptSocket;
return [[LXSocket alloc] initWithCSocket: AcceptSocket];
}
return nil;
}
- (BOOL)connect:(NSString*)host port:(unsigned)port {
struct sockaddr_in clientService;
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr([host UTF8String]);
clientService.sin_port = htons(port);
if (connect(m_socket, (struct sockaddr *) &clientService, sizeof(clientService)) == -1) {
printf("Client: connect() - Failed to connect.\n");
return NO;
} else return YES;
}
- (BOOL)sendBytes:(const void*)bytes length:(unsigned)len {
int sent = send(m_socket, bytes, len, 0);
return sent == len;
}
- (void*)readBytesWithLength:(unsigned)len {
void* buffer = malloc(len);
int received = recv(m_socket, buffer, len, 0);
if (received <= 0) {
free(buffer);
return NULL;
}
if (received < len) {
unsigned remaining = len;
char* ptr = buffer;
do {
remaining -= received;
ptr += received;
received = recv(m_socket, ptr, remaining, 0);
if (received <= 0) {
free(buffer);
return NULL;
}
} while (received < remaining);
}
return buffer;
}
- (BOOL)sendData:(NSData*)data {
const void* ptr = [data bytes];
BOOL succeeded = NO;
if ([self sendInt: [data length]])
if ([self sendBytes: ptr length: [data length]]) succeeded = YES;
//free(ptr);
return succeeded;
}
- (NSData*)readData{
unsigned len = [self readInt];
if (len > 0) {
void* ptr = [self readBytesWithLength: len];
if (ptr != NULL) {
NSData* data = [NSData dataWithBytes: ptr length: len];
free(ptr);
return data;
}
}
return nil;
}
- (BOOL)sendObject:(id)object {
NSMutableData* data = [[NSMutableData alloc] init];
NSKeyedArchiver* archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData: data];
[archiver encodeRootObject: object];
[archiver finishEncoding];
BOOL succeeded = [self sendData: data];
[archiver release];
[data release];
return succeeded;
}
- (id)readObject {
NSData* data = [self readData];
if (data != nil) {
NSKeyedUnarchiver* unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData: data];
id object = [[[unarchiver decodeObject] retain] autorelease];
[unarchiver release];
return object;
}
return nil;
}
- (BOOL)sendInt:(int)n {
return [self sendBytes: &n length: sizeof(n)];
}
- (BOOL)sendDouble:(double)n {
return [self sendBytes: &n length: sizeof(n)];
}
- (BOOL)sendInt64:(long long)n {
return [self sendBytes: &n length: sizeof(n)];
}
- (int)readInt {
void* buffer = [self readBytesWithLength: sizeof(int)];
if (buffer == NULL) return 0;
int n;
memcpy(&n, buffer, sizeof(n));
free(buffer);
return n;
}
- (double)readDouble {
void* buffer = [self readBytesWithLength: sizeof(double)];
if (buffer == NULL) return 0;
double n;
memcpy(&n, buffer, sizeof(n));
free(buffer);
return n;
}
- (long long)readInt64 {
void* buffer = [self readBytesWithLength: sizeof(long long)];
if (buffer == NULL) return 0;
long long n;
memcpy(&n, buffer, sizeof(n));
free(buffer);
return n;
}
- (BOOL)sendShort:(short)n {
return [self sendBytes: &n length: sizeof(n)];
}
- (short)readShort {
void* buffer = [self readBytesWithLength: sizeof(short)];
if (buffer == NULL) return 0;
short n;
memcpy(&n, buffer, sizeof(n));
free(buffer);
return n;
}
- (BOOL)sendLong:(long)n {
return [self sendBytes: &n length: sizeof(n)];
}
- (BOOL)sendUnsingedLongLong:(unsigned long long)n {
return YES;//return [self sendBytes: &n length: sizeof(n)];
}
- (long)readLong {
void* buffer = [self readBytesWithLength: sizeof(long)];
if (buffer == NULL) return 0;
long n;
memcpy(&n, buffer, sizeof(n));
free(buffer);
return n;
}
- (void)sendString:(NSString*)string {
const char* s = [string UTF8String];
int len = strlen(s);
[self sendInt: len];
[self sendBytes: s length: len + 1];
}
- (NSString*)readString {
int len = [self readInt];
char* s = [self readBytesWithLength: len + 1];
NSString* string = [NSString stringWithUTF8String: s];
free(s);
return string;
}
- (NSString*)resolveHostName:(NSString*)hostName {
struct hostent* host = gethostbyname([hostName UTF8String]);
if (host == NULL) {
printf("Error resolving host name: %d\n", strerror(errno));
return nil;
} else {
struct in_addr* addr = (struct in_addr*) host->h_addr_list[0];
char* ip = inet_ntoa(*addr);
return [NSString stringWithUTF8String: ip];
}
}
- (void)dealloc {
close(m_socket);
[super dealloc];
}
@end
一些进一步的结果(记住传递的原始十六进制字符串是 0x01、0x00、0x07、0x7f、0x00、0x00、0x01、0x00、0x03、0x40)(0100077f000001000340):
id *object = [socket readBytesWithLength:10];
NSLog(@"return data %X",*object); = return data 7F070001
NSLog(@"return data %llu",*object); = return data 45080838145
double object = [socket readInt64];
NSLog(@"return data %X",*object); = return data F0700010
NSLog(@"return data %llu",*object); = return data 4823355235012444176 (hex: 0x42F00007F0700010)
最佳答案
您可能必须编写一个特殊的函数来以这种方式打印,因为您需要循环遍历字符:
- (NSString *)bytesToString:(char *)bytes length:(int)length {
// Each byte is 2 hex characters, plus we need a ':' between each byte
NSMutableString *result = [NSMutableString stringWithCapacity:2*length + length - 1];
for (int i = 0; i < length; i++) {
if (i < length - 1) {
[result appendFormat:@"%02X:", bytes[i]];
}
else {
[result appendFormat:@"%02X", bytes[i]];
}
}
return result; // Result is auto-released
}
定义此函数后,您应该能够按如下方式记录:
NSLog(@"return data %@",[self bytesToString:object length:sizeof(connectByteArray)]);
关于objective-c - 显示从套接字连接发送的十六进制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7176393/
我有一个消息 static int[] message = { 0x01, 0x10, 0x00, 0x01, // port addres 01 - 08
如何将十进制转换为以下格式的十六进制(至少两位,零填充,不带 0x 前缀)? 输入:255 输出:ff 输入:2 输出:02 我尝试了 hex(int)[2:] 但它似乎显示了第一个示例而不是第二个示
这个问题已经有答案了: 已关闭12 年前。 Possible Duplicate: Large numbers in Pascal (Delphi) 我正在尝试将 66 位值转换为十进制。 我注意到d
给定一个十进制数字列表,如何将每个数字转换为其等效的十六进制值,反之亦然? 例如: (convert2hex 255 64 64); ->(FF 40 40) (convert2dec FF 40 4
var color = Math.floor(Math.random() * 16777215).toString(16); var hex = Number.parseInt(col
我一直被教导 0-9 代表 0 到 9 的值,A、B、C、D、E、F 代表 10-15。 我看到这种格式 0x00000000,它不适合十六进制模式。有没有导游或导师可以解释一下? 我在谷歌上搜索了十
我目前正尝试像十六进制编辑器一样将文件读取为十六进制值。为了解释这个问题,让我们假设我有一个test.txt,里面有一个简单的“Hello world”。我正在尝试使用接近以下代码的程序以十六进制形式
我正在尝试获取元素背景颜色 $(document).ready(function(){ $.each('.log-widget',function(){ console.log($(t
0x40130020的十六进制值是 2.296883 的浮点值, 使用本网站 http://gregstoll.dyndns.org/~gregstoll/floattohex/ .这如何实现到 Lu
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,vis
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
谁能解释一下我们如何计算十六进制浮点常量的值。我在看书,发现0x0.3p10代表值192。 最佳答案 指数仍以十进制表示,但底数为二,尾数为十六进制。 所以 0.3P10 是 (3 × 16−1) ×
我正在尝试创建一个标签云,需要帮助来创建一个函数来计算应用于每个标签链接所需的颜色。 我有 3 个变量: 单个标签重要性(从 0.1 到 1) 最大(最重要)的标签颜色(十六进制代码,例如“fff00
大家好,我想发送尽可能短的字符串/值。如果我有以下内容 1)l23k43i221j44h55uui6n433bb4 2)124987359824369785493584379 3)kla^askdja
我知道你会写... background-color: #ff0000; ...如果你想要红色的东西。 你可以写... background-color: rgba(255, 0, 0, 0.5);
我有一些传递地理位置坐标的二进制数据流 - 纬度和经度。我需要找到它们编码的方法。 4adac812 = 74°26.2851' = 74.438085 2b6059f9 = 43°0.2763'
我想从 my_table 中选择 family,其中 family LIKE '%HEX(9D)' 家庭十六进制格式以 9D 十六进制结尾 我将excel文件转换为sqlite数据库但是 我的一些数据
我有一组二进制配置文件,每个文件有三个版本——每个文件的原始版本和两个不同修改的版本。我需要能够同时看到两个版本和原始版本之间的差异。 我需要的是一个二进制文件的三向差异工具。通过相当费力的谷歌搜索,
我正在尝试将(可变长度)十六进制字符串转换为带符号整数(我需要正值或负值)。 [Int16] [int 32]和[int64] 似乎可以在2,4+字节长的十六进制字符串上正常工作,但我在使用3个字节的
如何将十六进制的 unicode 写入 Facebook“您在想什么”框? 我尝试过写: \u00B9 "\u00B9" ¹ "¹" 到目前为止没有任何效果 (让我补充一下,我是在 M
我是一名优秀的程序员,十分优秀!