AsyncSocket可以连接到Java Socket服务器但不能写入数据

时间:2011-04-18 16:17:59

标签: java iphone sockets asyncsocket

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

1 个答案:

答案 0 :(得分:0)

好吧,你没有向控制台输出任何其他东西。您将其发回给客户:

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

如果你telnet到你的服务器并输入内容,你会看到你收到你输入的内容。

我认为你在寻找:

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