1
我想做一個簡單的服務器客戶端程序,它允許我將滑動條值從IOS客戶端發送到java服務器。我現在被卡住了,因爲我收到了我不知道如何處理的數據。我正在尋找一點指導。我將提供迄今爲止我所擁有的客戶端和服務器端代碼。我想了解更多關於套接字,並感謝我能得到任何幫助。現在我只想從IOS發送一個字符串並將其打印到java中的控制檯。我只想從一個測試字符串開始。我的最終目標是通過將float值轉換爲字符串然後返回到java端來將滑塊的值發送給服務器。ios滑塊數據到java服務器使用TCP套接字
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);
}
}
}
據我所知,連接的處理方式是非常草率和欣賞任何建議。 我可以打通到Java方面的一些數據來當我移動IOS上的滑塊,它看起來像這樣在控制檯:
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 = [email protected]
waiting for socket accept
socket accepted and returns: Socket[addr=/127.0.0.1,port=61578,localport=7896]
inputStream = [email protected]
這是[email protected],我不知道如何處理。