| View previous topic :: View next topic |
| Author |
Message |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Thu Oct 11, 2007 5:18 pm Post subject: winsock question |
|
|
I don't get this code. How are you receiving "hello" if buf doesn't equal "hello"?
| Code: |
#include <windows.h> //Required for socket init
#include <iostream>
int main(){
char buf[256];
WSAData wsdata; //Declare WSAData
WORD wsver=MAKEWORD(2, 0); //We want Winsock 2.0
int nret=WSAStartup(wsver, &wsdata); //Pass version 2.0 and pointer to implement
if(nret != 0){ //Init failed
/*A successful return value should be 0 */
std::cout<<"Startup failed, error code: "<<WSAGetLastError(); //Returns error code
WSACleanup(); //Cleanup Winsock library
return -1;
}
std::cout<<"Init success\n";
SOCKET kSock=socket(AF_INET, SOCK_STREAM, 0);
if(kSock == INVALID_SOCKET){
std::cout<<"Socket init failed";
return -1;
}
std::cout<<"Socket initialized\n";
sockaddr_in sin;
sin.sin_port=htons(808); //Connect to port 80
sin.sin_addr.s_addr=inet_addr("127.0.0.1"); //Connect to localhost
sin.sin_family=AF_INET;
if(connect(kSock,(sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR){ //Check the condition
std::cout<<"Connect failed, error: "<<WSAGetLastError(); //Returns error code
WSACleanup(); //Cleanup the library
return -1;
}
std::cout<<"Connection successful!\n";
recv(kSock, buf, sizeof(buf), 0); //Receive "Hello" from server
std::cout << buf;
closesocket(kSock);
return 0;
}
|
_________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Thu Oct 11, 2007 5:25 pm Post subject: |
|
|
I will now preform witchcraft to answer your question.
Get this, the data you recieve with the recv() function fills buff[] with the text you send it over the internet! Who would of thought that is what winsock does!
/witchcraft
I bet I blew up your mind.
As a side note, google and MSDN are your friends, you don't need to come here for absolutely every single problem you have. Use the forum as a last resort.
|
|
| Back to top |
|
 |
appalsap Moderator
Reputation: 0
Joined: 27 Apr 2006 Posts: 6753 Location: Pakistan
|
Posted: Thu Oct 11, 2007 5:25 pm Post subject: |
|
|
"Hello" meaning the first message; a greeting, not literally "Hello"
|
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Thu Oct 11, 2007 5:31 pm Post subject: |
|
|
I know appalsap and Flyte stop being so sarcastic. And I'm sorry I keep coming to hte forum T.T
Oh btw, so if I were to make a chat program. How would that work. I mean like if someone click the send button send it to the other program and then somehow I have that person start receiving data? But I'm looking it up on google right now.
_________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
appalsap Moderator
Reputation: 0
Joined: 27 Apr 2006 Posts: 6753 Location: Pakistan
|
Posted: Thu Oct 11, 2007 5:32 pm Post subject: |
|
|
you send them the data with send() and they get it with recv()
|
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Thu Oct 11, 2007 5:44 pm Post subject: |
|
|
| oib111 wrote: | I know appalsap and Flyte stop being so sarcastic. And I'm sorry I keep coming to hte forum T.T
Oh btw, so if I were to make a chat program. How would that work. I mean like if someone click the send button send it to the other program and then somehow I have that person start receiving data? But I'm looking it up on google right now. |
You would have 2 threads. They would both loop endlessly, one would check if there is data to receive, receives the data with recv() then places it into a buffer for the program to use. The other thread would be dedicated to sending, though this thread is optional depending on the program (you could have the send() built into the button click).
Also, I will not stop being sarcastic.
|
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Thu Oct 11, 2007 5:47 pm Post subject: |
|
|
Lol fine. But can I be an arbitrary jackass?
Edit:
Btw, I had an idea that I would have to loop. And I would also like to know how to connect to a certain ip. Like instead of putting 127.0.0.1 in the stuct member host could I put their IP address. And how am I supposed to get it.
_________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Thu Oct 11, 2007 6:31 pm Post subject: |
|
|
ask them for it?
and yes you can, but they have to have a server open on that port number or you can't connect and you shall phail
_________________
|
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Thu Oct 11, 2007 7:25 pm Post subject: |
|
|
T.T Well on aim or msn how do they do it?
_________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
DeltaFlyer Grandmaster Cheater
Reputation: 0
Joined: 22 Jul 2006 Posts: 666
|
Posted: Thu Oct 11, 2007 7:46 pm Post subject: |
|
|
Those clients connect to a central predefined server with a definite IP. The server then does the job of the middleman to exchange information.
_________________
Wow.... still working at 827... what's INCA thinking?
zomg l33t hax at this place (IE only). Over 150 people have used it, what are YOU waiting for? |
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Thu Oct 11, 2007 10:32 pm Post subject: |
|
|
| DeltaFlyer wrote: | | Those clients connect to a central predefined server with a definite IP. The server then does the job of the middleman to exchange information. |
Not always. If you have to option turned on, you can directly connect to the person and the only purpose the servers have is to maintain login and to receive their IP address in the first place. It basically turns MSN into a small P2P program.
|
|
| Back to top |
|
 |
Robotex Master Cheater
Reputation: 0
Joined: 05 Sep 2006 Posts: 378 Location: The pizza country!
|
Posted: Fri Oct 12, 2007 6:13 am Post subject: |
|
|
| Flyte wrote: | You would have 2 threads. They would both loop endlessly, one would check if there is data to receive, receives the data with recv() then places it into a buffer for the program to use. The other thread would be dedicated to sending, though this thread is optional depending on the program (you could have the send() built into the button click).
|
There are the non-blocking sockets, they won't create any overhead and extra work caused by the threads, but it requires you to make a queue for the data.
| Quote: |
Use a combination of blocking and non-blocking socket operations. The ability to switch between blocking and non-blocking modes "on the fly" provides a powerful and convenient way to perform socket operations. Note that the warning regarding blocking sockets also applies here. |
_________________
ASM/C++ Coder
Project Speranza lead developer |
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Fri Oct 12, 2007 8:31 am Post subject: |
|
|
Ok, aside from sending and receiving data. I don't have a server lol. So how would I get the persons IP and then send and receive from that IP?
_________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
Robotex Master Cheater
Reputation: 0
Joined: 05 Sep 2006 Posts: 378 Location: The pizza country!
|
Posted: Fri Oct 12, 2007 11:39 am Post subject: |
|
|
if you are the host, you'll automatically get the client ip when you accept the socket (struct socketinfo)
_________________
ASM/C++ Coder
Project Speranza lead developer |
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Sat Oct 13, 2007 10:44 am Post subject: |
|
|
I have a question. I was watching a vb tutorial on just a simple chat with only one person. But it demands one of the people to have the server open and the other the client. What I'm wondering, is if you and your friend don't say like"Ok, you always run the server, I always run the client." Is there anyway to make it so it can detect if the two servers are open and then change one to the client. I was going to ask how msn did this and then I realized they probably have a server somewhere running that is used as a middle man in the process to send data from each client, also connecting each otehr to the ip and port.
_________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
|