AsyncSocket可以连接java Socket服务器但无法写入数据
每次我调用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,您没有向控制台输出任何其他内容。您将其发送回客户端:
如果您远程登录到您的服务器并输入一些内容,您将看到您返回了您输入的内容。
我想您正在寻找:
Well, you're not outputting anything else to the console. You're sending it back to the client:
If you telnet to your server and type something, you'll see you get back what you typed.
I think you're looking for: