每次我调用 upLoad,Java 控制台打印接受,但没有别的,数据传输了吗?
我将writedata调用改为didConnectToHost,现在客户端说数据已经传输了,但是如何在Java Server控制台中显示这些数据呢?
这是我的 Java 服务器代码:
import java.net.*;
import java.io.*;
public class Server
{
private ServerSocket ss;
private Socket socket;
private BufferedReader in;
private PrintWriter out;
public Server()
{
try
{
ss = new ServerSocket(8080);
while (true)
{
socket = ss.accept();
System.out.println("accepted");
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(),true);
String line = in.readLine();
out.println("you input is :" + line);
out.close();
in.close();
socket.close();
}
}
catch (IOException e)
{}
}
public static void main(String[] args)
{
new Server();
}
}
这是我的 iPhone 客户端代码:
-(IBAction)upLoad{
NSError* error;
NSString* hostIP = @"127.0.0.1";
UInt16 port = 8080;
[aSocket connectToHost:hostIP onPort:port withTimeout:-1 error:&error];
NSData *dataToUpload = [@"testxyz" dataUsingEncoding:NSUTF8StringEncoding];
}
#pragma mark -
#pragma mark socketdelegate
- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
{
connectionStatus.text = @"connected to server!";
NSData *dataToUpload = [@"testxyz" dataUsingEncoding:NSUTF8StringEncoding];
[sock writeData:dataToUpload withTimeout:-1 tag:0];
}
- (void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag
{
connectionStatus.text = @"write complete!";
}