Avatar of wxhuang
wxhuang
 asked on

Need Kind Help on winsock programming

Hi,

I have a Problem in my programming in winsock porgramming. In the program, I try to implement communication between a client and a server  so I put the functions of server's and client's together. By running two separate processes independently, one play a role as a server and the others as a client. After establishing the connection between them successfully,
the server fail to send the data to the client. However the client can send the data to the server successfully. That make me confused. Hope to get help from anybody who can set it.

The method in my program is as follows:

1 Establishing the server-side socket by running member functions of CAsyncSocket: create(Port) and Listen();
2 Establishing the client-side socket by running member functions of CAsyncSockte: create() and Listen(IPAddr,Port);(Looks successfully by get echo from server's OnAccept() and client's OnConnect() ).
3 Client send data to Server. (No echo from server's OnReceive());
4 Server send data to client. (Get echo from client's OnRecieve() and data).

The step 3 always doesn't work. I don't know why.
Can anybody do me a favor?

Thanks

William
 
Programming Languages-OtherProgramming

Avatar of undefined
Last Comment
wxhuang

8/22/2022 - Mon
somersx2000

It's hard to tell without some example code but it sounds as though both the "client" and the "server" are effectively acting as servers - in the sense of creating a listening socket which the other party connects to. Your points 1 and 2 certainly seem to indicate that both client and server do a listen().

There are many good reasons why you might be doing this, but the more normal model (as in HTTP, SMTP, POP3 etc., though not non-passive FTP) is that the server creates a socket, and listens, and clients connect to that socket. In other words: the server uses a create() function and a listen() function, and the client uses a create() function and a connect() function. You've then got a bi-directional pipe which either party can use to send data down.

It's unusual for both parties to create a listening socket, and it would be easier to help if we knew why you're doing this.
wxhuang

ASKER
Hi somersx2000,

Thank you for your reply. It seems to me that I have made some spelling mistake in my question when I typed it. Actually the Listen(IPAddr,Port) in the point 2 is Connect(IPAddr,Port) in the real case. It is still a client-server modal. This programm is just for updating of former program which uses socket to communicate with local and remote user. So that both functions of client and server are necessary. This program suppose to work properly  not acutally not.  I have no ideas how to find problem. I really hope to get your further help. Thank you.

William
somersx2000

I assume you've checked the return value of the client-side Send() in stage 3...
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
wxhuang

ASKER
exactly. The value is the same as sending out.

William
wxhuang

ASKER
I use the same function on both client and server. In the client side it was worked properly.

William
somersx2000

That leaves three main areas (though lots of other less likely ones) where the problem could lie:

1. "Infrastructure" problems, including bugs or shortcomings in the MFC classes.
2. Subtle differences in the details of your client-side and server-side code, which you haven't yet spotted.
3. Interference from things like personal firewall software, which could be blocking the "outbound" communication from client to server, but not vice versa.

However, item 2 is by far the most likely.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
splat

I used to have this happen when I opened two sockets on the same machine if both used the same port.  As a quick test, make the client and server use different ports and see if the problem still exists (remember to let the client listen on the client port but send data to the server port, likewise let the server listen on the server port but send data on the client port).  If the problem goes away, you need to setup address/port sharing for the socket if you plan to have both running on the same machine.  If the problem still exists, post your code so someone can look at it.
wxhuang

ASKER
Hi splat,

You are right. When I runing the program in the different machines connected to the same LAN, It works properly!
Thank you a lot , and thanks somersx2000 as well.

It is necessary for me to running the program in the same machine. You have pointed out the way of sharing the address/port.
Could you tell me how to implement it? Thank you!

William
splat

Call SetSockOpt and use this parameter: SO_REUSEADDR
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
wxhuang

ASKER
Thank you. I will try it later.
wxhuang

ASKER
Hi splat,

I have tried several time to use SO_REUSEADDR but not success. Could you please tell me how to use it by using  CAsyncSocket class?

Thanks

William
wxhuang

ASKER
Hi,splat

I have tried several time to use SetSockOpt in CAsyncSocket class but not success. Could you please tell me how to use it in CAsyncSocket?

Thanks

William
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
wxhuang

ASKER
Hi,splat

I have tried several time to use SetSockOpt in CAsyncSocket class but not success. Could you please tell me how to use it in CAsyncSocket?

Thanks

William
splat

You probably have a socket object declared somewhere--something like:

CASyncSocket mySocket;

You should then be able to call this function to enable address reusage:

BOOL optionValue = TRUE;
mySocket.SetSockOpt( SO_REUSEADDR, ( void* )&optionValue, sizeof( optionValue ) );
wxhuang

ASKER
Hi splat,

I have coded the program almost the same yours. But it still doesn't work. Could please find mistakes in following program:


#define m_strName "127.0.0.1"
#define m_iPort   10778       

CAsyncSocket m_sListenSocket;         // Used by server to listen to the connection information coming from a client.
CAsyncSocket m_sConnectSocket;      //Used by both client and server to transfer data.


client side code:

         m_sConnectSocket.Create();
         m_sConnectSocket.Connect(m_strName, m_iPort);
      ...


server side code:

        m_sListenSocket.m_hSocket = socket(AF_INET,SOCK_STREAM,0);
        BOOL useid = true;
        m_sListenSocket.SetSockOpt(SO_REUSEADDR,(void *)&useid, sizeof(BOOL),SOL_SOCKET);
        m_sListenSocket.Bind(m_iPort);
        m_sListenSocket.Listen();
        ...


Thanks

William

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER CERTIFIED SOLUTION
splat

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
CleanupPing

wxhuang:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
wxhuang

ASKER
Thanks every one for your kind help.