Solved

an easy winsock question

Posted on 1999-01-04
14
243 Views
Last Modified: 2010-04-02
I have what I think is an easy question about
the winsock control. I don't know what to write
to connect. I want to connect to port 1001 and
the computers name I want to connect to is
"FREDRIK"  I have tried
to write:
winsock1.connect("FREDRIK",1001);
but I don't get it to work. Please help me.
Thanks in advance.

0
Comment
Question by:otroligafredde
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
  • +1
14 Comments
 
LVL 4

Expert Comment

by:nil_dib
ID: 1181451
Which compiler?
What is *winsock1*?
0
 
LVL 3

Author Comment

by:otroligafredde
ID: 1181452
I'm using visual studio 6.0
and winsock1 is the variable for the winsock
control. it's really m_client.

0
 
LVL 4

Expert Comment

by:nil_dib
ID: 1181453
So you are using CAsyncSocket class?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:otroligafredde
ID: 1181454
No I'm not using CAsyncSocket class.
Should I?

I don't really know how to do this. I just
inserted the winsockcontrol in my project
and now I'm trying to use it with the functions
that comes with it. Listen, Accept, OnConnectionRequest
and so on....

Is there more I need to know?
 
0
 
LVL 4

Expert Comment

by:nil_dib
ID: 1181455
Ok, sorry I'm a bit slow this morning ...
Yes there is more you should know!
I think the best way is to read the chapters
Windows Sockets: Background
Windows Sockets: Sequence of Operations
Windows Sockets: Socket Notifications
Windows Sockets: Ports and Socket Addresses
.
> but I don't get it to work.
What is the exact behaviour?
Your server is listening?



0
 
LVL 3

Author Comment

by:otroligafredde
ID: 1181456
I think that my server application is listening
but I can't tell for sure, cause I can't get my client
application to work.
I have used the winsock control in visual basic before
so I know how it works. it's just that it's much more complicated
in vc++.

But if I want to read the chapters that you wrote about
should I look in any special book? My msdn library doesn't
work.

the problem I have is that it's something wrong with the parameters that I'm passing to the Connect function.
It wants a const &var or something, I don't know what to write.

I don't know if this comment made my question any clearer.

0
 
LVL 4

Expert Comment

by:nil_dib
ID: 1181457
Ok,
> the problem I have is that it's something wrong with the
> parameters that I'm passing to the Connect function.
that means you can not complie your client?
Did you try other ports?
Which kind of protocol do you use (in case of TCP/IP did you try
the whole name of you server: e.g. "FREDRIK.domain.com")?
> I think that my server application is listening
> but I can't tell for sure, cause I can't get my client
> application to work.
So you are not sure, whether the problem is in the server or the
client!?


0
 
LVL 4

Expert Comment

by:nil_dib
ID: 1181458
I'm really slow this day!
Socket port 1001 is invalid!
Ports < 1024 are reserved for privileged processes (e.g. root).
Try a port > 1024.

0
 
LVL 11

Expert Comment

by:alexo
ID: 1181459
And if you're using MFC (which you obviously do use) try asking in the MFC programming area instead of the C++ area.  You'll usually get more/better responses there.
0
 
LVL 3

Author Comment

by:otroligafredde
ID: 1181460
I don't think it's the port number that's causing
the problem. I have changed it now by the way.
It's the way I'm calling the Connect function thats
wrong.
I get a compile error when I call it.

I call it like this:

m_klient.Connect(FREDRIK, 1025);
and I have tried it like this:
m_klient.Connect("FREDRIK", 1025);

but it wants a 'const struct tagVARIANT' instead of FREDRIK or "FREDRIK"

Do you know what I do wrong?

Thanks in advance!

0
 
LVL 3

Author Comment

by:otroligafredde
ID: 1181461
Yes I do use TCP/IP

0
 
LVL 4

Expert Comment

by:nil_dib
ID: 1181462
Try

COleVariant myVarinat( "FREDRIK" );
.
m_klient.Connect(myVarinat, 1025);
.
 
0
 

Accepted Solution

by:
mbequette earned 100 total points
ID: 1181463
Listed below is an example of a working C++ client using port 600(but choose any port you want, just realize that some ports may be used exclusively by other programs e.g. port 80 for web servers) is as follows:

I am using the CSocket MFC class on the client because typically you would want a client to block.  

CSocket ClientSocket;
//Initialize Sockets
      AfxSocketInit();
            
      ClientSocket.Create();//creates the socket
   if(ClientSocket.Connect(m_ipaddress,600))//connects to server
{
     NumBytes=ClientSocket.Receive(&Response,sizeof(int));
            if(NumBytes)
            {
                  if(Response==1000)
                  {
                         SendData=1000;
                    AfxMessageBox("Received proper data");
            ClientSocket.Send(&SendData,sizeof(int))
                        }
                  
                  else            
            AfxMessageBox("Didn't Receive any data");
                              
                                              This should allow you to connect to a server,  However, you will still need to set up the basics of the protocol for sending and receiving data, which is very simple to do also.  Please note in this example after you connect to the server the server responds with either a value of 1000 to determine that it connected correctly.  The client responds with a 1000 to the server.  if you can't modify the server code(i.e. connecting to someone elses server) then you will need to determine how data is handled by the server and modify the way you send and receive data on the client.
if you want a name to use instead of an ip address for a user to locate in an edit box, then you can place the name in the hosts file and translate the name to an ip address.  To send data call the method send, and to receive call the method receive as in the above example.
Hope this helps,
mb


0
 

Expert Comment

by:mbequette
ID: 1181464
In addition to what I have already submitted, if you want to use a computer name located somewhere on the network, you can place the computer name in the hosts file(NT) along with its ip address, but in the sockets connect function use the server's IP address for the server(i.e. FREDRIK's ip address).  If FREDRIK is the same machine as the client use "localhost" as the name.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question