本文介绍了ServerSocket java没有读取inputStream?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个服务器是java这里是代码:

  public mainClass()
{
try
{
ss = new ServerSocket(8080);

while(true)
{
socket = ss.accept();
System.out.println(It is accept!);

in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// out = new PrintWriter(socket.getOutputStream(),true);

line = in.readLine();
System.out.println(you input is:+ line);
//out.close();
in.close();
socket.close();

}

}
catch(IOException e)
{

}

,我使用iPhone应用程序作为客户端。
现在我的问题是,服务器没有读取输入流,而应用程序正在运行在iphone上..但一旦应用程序终止,java程序打印出已发送到服务器的字符串。 。

   - (void)viewDidLoad {
[super viewDidLoad];

socket = [[LXSocket alloc] init];

if([socket connect:@10.211.55.2port:8080]){

NSLog(@socket has been created
}
else {
NSLog(@socket not not create created);
}

@try {

[socket sendString:@Hi这是第二个测试];
}

@catch(NSException * e){
NSLog(@无法发送数据);
}


[super viewDidLoad];

}



b

解决方案

根据我自己的经验, readLine 想法,尤其是在使用不同的语言和平台时,更好的方法是使用 InputStreamReader 及其方法,双方同意每次发送的长度。



再次,我没有提及,只有我的经验。 / p>

另外,看看你的代码,你发送一个没有换行符的字符串: [socket sendString:@Hi这是第二个测试 ]; 可能添加 \\\
到底会为你解决它。


I have written a server is java here is the code:

public mainClass()
{
    try
    {
        ss = new ServerSocket(8080);

        while (true)
        {
            socket = ss.accept();
            System.out.println("It is accept!");

            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            //out = new PrintWriter(socket.getOutputStream(),true);

            line = in.readLine();
            System.out.println("you input is :" + line);
            //out.close();
            in.close();
            socket.close();

        }

    }
    catch (IOException e)
    {

    }

and I am using an iPhone application as the client.now what my problem is that the server is not reading the inputstream while the appication is running on the iphone.. But as soon as the application is terminated the java program prints out the String which has been sent to the server..Not sure what is happening here..sorry if this is not a good question..

- (void)viewDidLoad {
[super viewDidLoad];

socket = [[LXSocket alloc]init];

if ([socket connect:@"10.211.55.2" port:8080]) {

    NSLog(@"socket has been created");
}
else {
    NSLog(@"socket couldn't be created created");
}

@try {

[socket sendString:@"Hi This is a second test"];
}

@catch (NSException * e) {
NSLog(@"Unable to send data");
}


    [super viewDidLoad];

}

thanks,TC

解决方案

From my own experience, readLine is not a good idea, especially when working with different languages and platforms, a better approach will be to use InputStreamReader and its read(char[] buff) method, and agree on both sides regarding the length to be sent each time.

Again, I have no reference to that, only my experience.

Also, looking at your code, you send a string without a new line character: [socket sendString:@"Hi This is a second test"]; maybe adding \n at the end will solve it for you.

这篇关于ServerSocket java没有读取inputStream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 19:06