SFML, why can't I create a connect socket function outside of main?

Ideally i would like to use this model but im having difficulty, i spent ages fiddling with the code til I realised that I cannot do it this way, im annoyed though, is there a way in which this will work?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

void loginfunction (string,int);//function prototypes
void sendfunction();
void recivefunction();

int main
{

loginfunction (cplusplus.com,80);

loop()
{
thread1(sendfunction);
thread2(recievefunction);
}

}

void loginfunction(string x,int y)
{
tcpsocket.connect(x,y);
}

void recivefunction()
{
tcpsocket.recieve(bunnfer,sizeof(buffer),sizt)
cout<<buffer;
}
Last edited on
You're not exactly being very clear on what you're doing or why you think you can't do it that way.
The psudo code is exactly what I want to do :/ is this not enough for you?
The pseudo code is pseudo code and doesn't tell me why what you're trying to do doesn't work. Which brings me back to this point in your OP:

I realised that I cannot do it this way


What made you come to that conclusion? Please elaborate. "It doesn't work" or similarly vague descriptions do not tell us anything about what's going wrong and makes it hard/impossible to diagnose the problem.




I can only surmise that you're having a threading issue/race condition due to not putting accesses to your socket behind a mutex. But I don't know if you're actually doing that or if you just omitted it from the pseudo-code because it's pseudo-code.



So yeah.. .when having a problem.... it really helps to post actual broken code. And to explain exactly why you think it's broken. Otherwise I just have to make all sorts of guesses and assumptions.
Last edited on
closed account (N36fSL3A)
devon we know it's broken, but just because you have pseudo code doesn't mean anything.

We know what you want to do from it, but we can't just magically know what your source code looks like.

If you're declaring the variable inside a function, then you can't use it inside another. If you want your variable to be used inside of another function, then you must make it global.
I arghh brain is so fried, cant even articulate, but yeh its some kind of recieving and sending in a thread causing issues somehow even with mutexes,
but my code is identicle to that pseudo code, everything else is flak.

I have this basic code, it gets further than my other one does, but i used this to study the irc protocol, this gets so far then stops, right before joining to a new thread.

whats going wrong here exactly, i believe i will gain the necissary insight from the answer, despitet the codes not being the same
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <SFML/System.hpp>
#include <iostream>
#include <string>

sf::TcpSocket tcpsocket;
sf::IpAddress address = "pratchett.freenode.net";
bool flag = true;
int send;
std::size_t recieved = 0;
char buffer[200]="NICK revengebot\r\n USER guest 0 * :revengebot\r\n",message[200],buffer2[20000];

int main()
{
    sf::TcpSocket::Status stat;
    stat=tcpsocket.connect(address,6665);
    std::cout<<stat;
    tcpsocket.send(buffer,sizeof(buffer));
    std::cout<<"sent:"<<buffer<<std::endl;
    while(flag)
    {

        std::cout<<buffer2<<std::endl;;
        std::cout<<"1 for send 2 to close 3 to skip\n";
        std::cin>>send;
        std::cout<<stat;
        switch(send){
    case 1:
        {
        std::cout<<"input:";
        std::cin>>message;
        tcpsocket.send(message,sizeof(message));
        break;
        }
    case 2:
        {
        flag = false;
        break;
        }
    case 3:
        tcpsocket.receive(buffer2,sizeof(buffer2),recieved);
        break;
    default:
        break;
        }
    }
}


I will just read a good tutorial and then report back.
Last edited on
but my code is identicle to that pseudo code


Well then you need to use a mutex. If you are accessing a single object in multiple threads, accesses must be guarded.
OH WAIT I DO HAVE MUTEXES :/ why am my brain melting?
The code you posted has neither multiple threads nor mutexes. >_>


whats going wrong here exactly,


That's my question to you. Does it not do what you expect? What do you expect it to do and what does it actually do?

We can't solve a problem if we don't know what the problem is. Tell us the problem.
https://www.dropbox.com/sh/m5t7w2ysxdx4la8/EYmvPxHcEi

theres locks for now but no threads, sry but the code is impossible to slice up, the send and recieve sockets are in an experimental place, your right tho' should have given the code to start with but, the code is so big, cutting it wont help that much because a lot wud be out of context, so theres a link to the whole project :/
You still haven't said what the actual problem is. I've asked like 3 times.

I'm not going to pull teeth to help you.


sry disch, ive not been very clear, it stops at "found hostname" after getting ident response then it times out.

originally it seemed as though send and recieve and login were so seperate there wasnt any ouput data at all.

im getting race conditions or so it seems despite using mutexs and locks and no threads.

maybe its just in blocking mode but that shouldnt stop it from working.

I just dont see how winsock worked and sfml code doesnt, i have the same protocol set up, so i guess it may just be the irc protocol silly me.
Last edited on
tcpsocket.setblocking(false); -_-
Topic archived. No new replies allowed.