gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 05:46:54 25 4
gpt4 key购买 nike

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

最佳答案

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

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

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

我认为您正在寻找:

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

关于java - AsyncSocket可以连接java Socket服务器但无法写入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5705826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com