1

每次我调用 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!";
}
4

1 回答 1

0

好吧,您没有向控制台输出任何其他内容。您将其发送回客户端:

out.println("you input is :" + line); 

如果您 telnet 到您的服务器并输入一些内容,您会看到您找回了您输入的内容。

我想你正在寻找:

System.out.println("you input is :" + line); 
于 2011-04-18T16:30:02.570 回答