forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Reading from a Socket

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
Jai_Guru



Joined: 13 Aug 2006
Posts: 12

PostPosted: Sun Aug 13, 2006 8:11 am    Post subject: Reading from a Socket Reply with quote

I'm trying to create a client program that connects to a server on my computer.

When I try to receive on the PSP a string sent by the server I only receive one character at first and then the rest of the string.

This is the code I use:
Code:

                while(1) {
                    bzero(&buffer, sizeof(buffer));
                    n = read(sockfd,buffer,255);
                    printf("%d bytes read\n", n);
                    if (n < 0)
                        printf("ERROR reading from socket");
                    printf("%s\n",buffer);
                }


For example, if y send something lilke "Hello PSP\n" the output on the PSP is something like:

Code:

1 bytes read
H
9 bytes read
ello PSP


How can I receive all the data sent by the server together?
Back to top
View user's profile Send private message
siberianstar



Joined: 22 Jun 2006
Posts: 70

PostPosted: Sun Aug 13, 2006 9:42 am    Post subject: Reply with quote

Using 'read' the simplest way is to read each time a char

Code:

char buffer[max_size];
int offset = 0;
char ch;
..
if (read(sockfd, &ch, 1) < 0) .. error, close socket ..
if (ch == '\n')
{
  buffer[offset] = 0;
  offset = 0;
  parse_command(buffer);
}
else buffer[offset++] = ch;
..


but this will be slow, a faster way is to read buffer in chunk.
Back to top
View user's profile Send private message
Jai_Guru



Joined: 13 Aug 2006
Posts: 12

PostPosted: Sun Aug 13, 2006 9:54 am    Post subject: Reply with quote

How can I read buffer in chunk?

When I try to do it, the string sent by the server is split as I explained on my first post.

What alternatives do I have other than using 'read' ?
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group