Solved

CSockets for Client-Server operation

Posted on 2003-11-20
7
329 Views
Last Modified: 2013-11-20
Hi
I am using CSockets for a Client-Server model.( both sides must be able to send and receive )

On the Client side I have a class that extends the CSocket class and overrides the following methods:
       void OnConnect(int nErrorCode);
       void OnSend(int nErrorCode);
       void OnReceive(int nErrorCode);
       void OnClose(int nErrorCode);
       void OnAccept(int nErrorCode);

On the Server side I have a class that extends the CSocket class and overrides the following methods:
       void OnSend(int nErrorCode);
       void OnReceive(int nErrorCode);
       void OnClose(int nErrorCode);
       void OnAccept(int nErrorCode);

I can send data to the Server. But I also wish to receive something at the Client side from the server. Is it necessary for me to do a Listen on the client side ? I tried this but then I can never connect at all to the Server. I dont think I need to even add any code to the OnAccept() function on my client to spawn of another socket. My OnReceive() should be able to handle it in the context of the current socket.  But it doesnt seem to work. What am I doing wrong ?

Note that  sockets on both sides are created as :

BOOL Val = clientSocket.Create(clientPort);
BOOL Val = mySocket.Create(serverPort);

Help !
-P
0
Comment
Question by:prakash_gs
7 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 9786483
You need to have listening socket in the server. For each new connection you should create socket which talks with connected client.
Client application should have only one socket which both sends and receives data.

Server:
Listening socket - OnAccept: create client socket which sends and receives data.

Client - socket which sends and receives data.

Check out MFC Sample CHATSRVR.

0
 
LVL 48

Expert Comment

by:AlexFM
ID: 9786515
0
 
LVL 1

Expert Comment

by:rami_halim
ID: 9805512
no you don't need a listening socket on the client end, you could but you don't have to.

you should have listen and accepting socket at the server end.
the listen socket waits incoming connections and you accpt it like this

 AcceptSocket.accept ( listenSocket )

on the client end you would only have a clientSocket
and start Connect()
once the connection is established you can send and receive data on both ends..

are you using Async or nonAsync sockets ?..

0
 

Author Comment

by:prakash_gs
ID: 9809086
hi
i figured out my problem.
I wasnt doing a send on the spawned socket that is created on the server on the OnAccept().Instead I was trying to directly send using my parent Server socket. So there ends my travails for now.(I hope...)
Thank you guys.
-P
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 11984794
PAQed, with points refunded (125)

Computer101
E-E Admin
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to create frequencies of a variable from SAS dataset? 10 120
string initialization in java 11 109
lucky13 challenge 11 120
firstChar challenge 13 106
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

947 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now