AsyncSocket可以连接java Socket服务器但无法写入数据

发布于 2024-11-02 03:04:17 字数 1873 浏览 1 评论 0原文

每次我调用upLoad时,Java控制台都会打印accept,但没有其他内容,数据传输了吗?

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

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; 
    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(); 
    } 
} 

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情深如许 2024-11-09 03:04:17

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

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

如果您远程登录到您的服务器并输入一些内容,您将看到您返回了您输入的内容。

我想您正在寻找:

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

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); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文