我正在尝试制作一个简单的服务器客户端程序,它允许我将滑块值从 IOS 客户端发送到 Java 服务器。我现在被卡住了,因为我收到了我不知道如何处理的数据。我正在寻找一些指导。我将提供到目前为止的客户端和服务器端代码。我想了解有关套接字的更多信息,并感谢我能得到的任何帮助。现在我只想能够从 IOS 发送一个字符串并将其打印到 java 中的控制台。我只想从一个测试字符串开始。我的最终目标是通过将浮点值转换为字符串然后返回到 java 端,将滑块的值实时发送到服务器。
IOS客户端代码
#import "ViewController.h"
@interface ViewController () <NSStreamDelegate>
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self TcpClientInitialise];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)SliderDidChange:(id)sender {
UISlider *slider = (UISlider *)sender;
float val = slider.value;
self.SliderLabel.text = [NSString stringWithFormat:@"%f",val];
[self TcpClientInitialise];
NSString *response = @"HELLO1234";
NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSUTF8StringEncoding]];
[OutputStream write:[data bytes] maxLength:[data length]]; //<<Returns actual number of bytes sent - check if trying to send a large number of bytes as they may well not have all gone in this write and will need sending once there is a hasspaceavailable event
NSLog(@"Sent data on output stream");
[InputStream close];
[OutputStream close];
}
- (void)TcpClientInitialise
{
NSLog(@"Tcp Client Initialise");
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"127.0.0.1", 7896, &readStream, &writeStream);
InputStream = (__bridge NSInputStream *)readStream;
OutputStream = (__bridge NSOutputStream *)writeStream;
[InputStream setDelegate:self];
[OutputStream setDelegate:self];
[InputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[OutputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[InputStream open];
[OutputStream open];
}
...
...
...
和Java代码...
import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Handler;
public class TCPServer {
private static PrintStream outputStream;
@SuppressWarnings("deprecation")
public static void main(String[] args) {
System.out.println("Main Test");
ServerSocket statusServer = null;
String number = null;
DataInputStream inputStream = null;
outputStream = null;
Socket clientSocket = null;
// Try to open a server socket on port 7896
try {
statusServer = new ServerSocket(7896);
System.out.println("ServerSocket status server made");
}
catch (IOException e) {
System.out.println(e);
System.out.println("status server failed");
}
// Create a socket object from the ServerSocket to listen and accept
// connections.
// Open input and output streams
try {
while(true){
System.out.println("waiting for socket accept");
clientSocket = statusServer.accept();
System.out.print("socket accepted and returns: ");
System.out.println(statusServer.accept());
inputStream = new DataInputStream(clientSocket.getInputStream());
number = inputStream.toString();
System.out.print("inputStream = ");
System.out.println(number);
inputStream.close();
clientSocket.close();
//outputStream = new PrintStream(clientSocket.getOutputStream());
}
} catch (IOException e) {
System.out.println(e);
}
}
}
我了解处理连接的方式非常草率,并感谢任何建议。当我在 IOS 上移动滑块时,我可以获得一些通过 java 端的数据,它在控制台中看起来像这样:
Main Test
ServerSocket status server made
waiting for socket accept
socket accepted and returns: Socket[addr=/127.0.0.1,port=61576,localport=7896]
inputStream = java.io.DataInputStream@2891fa78
waiting for socket accept
socket accepted and returns: Socket[addr=/127.0.0.1,port=61578,localport=7896]
inputStream = java.io.DataInputStream@5b8767ad
这是我不知道如何处理的java.io.DataInputStream@xxxxxxxx。