开发者

AsyncSocket can connect to java Socket server but can't write data

every time i call upLoad, Java console prints accept, but nothing else, is the data transfered?

I changed the writedata call to didConnectToHost, and now the client say that the data has been transfered, but how can I show these data in Java Server console?

this is my java server code:

import java.net.*; 
import java.io.*; 

public class Server 
{ 
    private ServerSocket ss; 
    priva开发者_开发百科te 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(); 
    } 
} 

this is my iPhone Client code:

-(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!";
}


Well, you're not outputting anything else to the console. You're sending it back to the client:

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

If you telnet to your server and type something, you'll see you get back what you typed.

I think you're looking for:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜