POSIX threads

Post questions regarding programming in C/C++ in Linux/Unix.

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

POSIX threads

Postby OverRated » Sat Sep 19, 2009 1:41 am

...........
Last edited by OverRated on Mon May 17, 2010 1:40 am, edited 1 time in total.
OverRated
 
Posts: 41
Joined: Wed Oct 01, 2008 12:05 pm

Re: POSIX threads

Postby MXP » Mon Sep 21, 2009 1:45 pm

Why are they all receiving on the same buffer? Why not give each thread its own buffer?
Need information on a function I've posted? Chances are it's at the MSDN.
MXP
 
Posts: 6506
Joined: Mon Sep 22, 2003 5:27 pm

Re: POSIX threads

Postby OverRated » Mon Sep 21, 2009 3:49 pm

Even when i Declare Buffer inside the thread it still gives me invalid results. For soem reason buffer gets cut off and only holds the last 1/3 of the page.
OverRated
 
Posts: 41
Joined: Wed Oct 01, 2008 12:05 pm

Re: POSIX threads

Postby MXP » Tue Sep 22, 2009 1:36 pm

Is buffer large enough to hold the whole page? Is there any more data to be read from the socket after the first read?
Need information on a function I've posted? Chances are it's at the MSDN.
MXP
 
Posts: 6506
Joined: Mon Sep 22, 2003 5:27 pm

Re: POSIX threads

Postby OverRated » Wed Sep 23, 2009 5:23 pm

Yes buffer is more than large enough to hold the whole page. Just needs to store the page in buffer and search for some text thats all
OverRated
 
Posts: 41
Joined: Wed Oct 01, 2008 12:05 pm

Re: POSIX threads

Postby MXP » Thu Sep 24, 2009 1:55 pm

Is there more data to be read from the socket after you call recv()?
Need information on a function I've posted? Chances are it's at the MSDN.
MXP
 
Posts: 6506
Joined: Mon Sep 22, 2003 5:27 pm

Re: POSIX threads

Postby OverRated » Fri Sep 25, 2009 6:45 am

no not until it loops again and sends different data.
OverRated
 
Posts: 41
Joined: Wed Oct 01, 2008 12:05 pm

Re: POSIX threads

Postby OverRated » Mon Dec 14, 2009 6:08 am

^ How does that help me O.o im on linux so win32 wont work. Unless i run it through wine but i rather not do that..
OverRated
 
Posts: 41
Joined: Wed Oct 01, 2008 12:05 pm

Re: POSIX threads

Postby Alvaro » Mon Dec 14, 2009 7:05 am

It's a bot.
User avatar
Alvaro
Moderator
 
Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

Re: POSIX threads

Postby Alvaro » Mon Dec 14, 2009 7:23 am

Of course, sharing a global buffer across threads is not going to work.

This won't answer your question, but this code makes my eyes hurt:
Code: Select all
    for(i = 0; i < strlen(buffer) + 2;i++)

First of all, it looks like you may be reading past the end of the string in some cases. Also, unless the compiler is really smart, it will compute the length of the zero-terminated string in `buffer' every time. Similarly with using `strlen(word)'. Save those lengths in local variables at the beginning of the function, or just use std::string.

Also, that `search' function is messed up in general. Try `search("bababata", "babata")'.

You have to always check the results of system calls like `send' or `recv'. Network programming is very tricky, and all sorts of errors may happen. If you do detect an error, make sure to print out what the problem was, using errno. Sometimes `send()' and `recv()' will only send part of your message, and that's not even an error: It's part of normal operation in TCP. Also part of normal operation is getting an error with errno==EINPROGRESS or perhaps even errno==EAGAIN (with non-blocking sockets). I don't remember all the details, so check the documentation on exactly how it works. I like this book on the subject. If you can afford it or if you can find it in a library, read it.
User avatar
Alvaro
Moderator
 
Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

Re: POSIX threads

Postby OverRated » Mon Dec 14, 2009 9:46 pm

Code: Select all
int search(char* buffer,const char* word)
{
    unsigned int i = 0,
        j = 0;
    for(i = 0; i <= strlen(buffer);i++)
    {
        if(buffer[i] == word[j] && j != strlen(word))
            j++;
        else if(j == strlen(word))
            return (1);
        else
            j = 0;
    }
    return (0);
}


Fixed the search function. Ill buy that book probably. thanks.
OverRated
 
Posts: 41
Joined: Wed Oct 01, 2008 12:05 pm

Re: POSIX threads

Postby Alvaro » Mon Dec 14, 2009 9:54 pm

You still didn't fix the multiple calls to strlen for the same string... You could check
Code: Select all
word[j] != '\0'
instead of
Code: Select all
j != strlen(word)


EDIT: And your code still thinks that the string "bababata" does not contain the substring "babata".

You could also just write this:
Code: Select all
#include <cstring>

bool search(const char* buffer, const char* word) {
  return std::strstr(buffer, word)!=0;
}
User avatar
Alvaro
Moderator
 
Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

Re: POSIX threads

Postby jinguo.linux.gnu » Wed Apr 06, 2011 9:26 pm

Alvaro wrote:You still didn't fix the multiple calls to strlen for the same string... You could check
Code: Select all
word[j] != '\0'
instead of
Code: Select all
j != strlen(word)


EDIT: And your code still thinks that the string "bababata" does not contain the substring "babata".

You could also just write this:
Code: Select all
#include <cstring>

bool search(const char* buffer, const char* word) {
  return std::strstr(buffer, word)!=0;
}
:teach:
Everyone on this website,can tell me about the different the POSIX pthread and the Boost thread?
When we programming a project on Linux or Unix which one i will choice?
User avatar
jinguo.linux.gnu
 
Posts: 2
Joined: Wed Apr 06, 2011 8:58 pm


Return to Unix/Linux

Who is online

Users browsing this forum: No registered users and 1 guest