如何从客户端的服务器一次接收字符串?

时间:2014-01-14 22:09:22

标签: java objective-c sockets

在java服务器部分我一次发送单独的字符串,但在客户端部分我收到所有这些,就好像它们是一个字符串。我如何分开收到的每个字符串。 这是我的代码。

客户端部分:

uint8_t buf[1024];
unsigned int len = 0;
len = [inputStream read:buf maxLength:1024];
NSString *s;

    NSMutableData* data=[[NSMutableData alloc] initWithLength:0];
    [data appendBytes: (const void *)buf length:len];
    s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

     NSString *firstString;// save first string received here
     NSString *secondString;// save second string received here
     NSLog(@"%@",s);

服务器部分:

ServerSocket welcomeSocket=new ServerSocket(6789);
while(true){
try{
Socket connectionSocket=welcomeSocket.accept();
OutputStream outToClient=new BufferedOutputStream(connectionSocket.getOutputStream());
outToClient.write("sign up accepted you can now sign in\n".getBytes("UTF-8"));
outToClient.write("enter\n".getBytes("UTF-8")));
}
catch(Exception e){System.out.println(e)}
finally{
if(outToClient!=null){
outToClient.flush();
}

0 个答案:

没有答案
相关问题