第一次连接后我断开Socket,然后再接着一个新的CSocket *cs= new CSocket
然后cs.Create()
这里出错,GetLastError 得到值为183
请问这该怎么解决
7 个解决方案
#1
当文件已存在时,无法创建该文件。
#2
错误的意思我知道,但是为什么有这种错误,我该怎么避开
#3
#4
我自己顶下,求帮助
#5
要么析构之前的CSocket之后再构造新的CSocket,再CSocket.Create()
要么使用另一个CSocket
要么使用CSocket数组
要么使用另一个CSocket
要么使用CSocket数组
#6
Windows Sockets: Using Sockets with Archives
Home | Overview | How Do I | Sample
This article describes the CSocket programming model. Class CSocket supplies socket support at a higher level of abstraction than does class CAsyncSocket. CSocket uses a version of the MFC serialization protocol to pass data to and from a socket object via an MFC CArchive object. CSocket provides blocking (while managing background processing of Windows messages) and gives you access to CArchive, which manages many aspects of the communication that you would have to do yourself using either the raw API or class CAsyncSocket.
Tip You can use class CSocket by itself, as a more convenient version of CAsyncSocket, but the simplest programming model is to use CSocket with a CArchive object.
For additional information about how the implementation of sockets with archives works, see the article Windows Sockets: How Sockets with Archives Work. For example code, see the articles Windows Sockets: Sequence of Operations and Windows Sockets: Example of Sockets Using Archives. For information about some of the functionality you can gain by deriving your own classes from the sockets classes, see the article Windows Sockets: Deriving from Socket Classes.
Caution If you are writing an MFC client program to communicate with established (non-MFC) servers, don’t send C++ objects via the archive. Unless the server is an MFC application that understands the kinds of objects you want to send, it won’t be able to receive and deserialize your objects. For related material on the subject of communicating with non-MFC applications, also see the article Windows Sockets: Byte Ordering.
The CSocket Programming Model
Using a CSocket object involves creating and associating together several MFC class objects. In the general procedure below, each step is taken by both the server socket and the client socket, except for step 3, in which each socket type requires a different action.
Tip At run time, the server application usually starts first in order to be ready and “listening” when the client application seeks a connection. If the server is not ready when the client tries to connect, you typically require the user application to try connecting again later.
To set up communication between a server socket and a client socket
Construct a CSocket object.
Use the object to create the underlying SOCKET handle.
For a CSocket client object, you should normally use the default parameters to Create, unless you need a datagram socket. For a CSocket server object, you must specify a port in the Create call.
Note CArchive doesn’t work with datagram sockets. If you want to use CSocket for a datagram socket, you must use the class as you would CAsyncSocket — without an archive. Because datagrams are unreliable (not guaranteed to arrive and may be repeated or out of sequence), they aren’t compatible with serialization via an archive. You expect a serialization operation to complete reliably and in sequence. If you try to use CSocket with a CArchive object for a datagram, an MFC assertion fails.
If the socket is a client, call CAsyncSocket::Connect to connect the socket object to a server socket.
-or-
If the socket is a server, call CAsyncSocket::Listen to begin listening for connect attempts from a client. Upon receiving a connection request, accept it by calling CAsyncSocket::Accept.
Note The Accept member function takes a reference to a new, empty CSocket object as its parameter. You must construct this object before you call Accept. Keep in mind that if this socket object goes out of scope, the connection closes. Do not call Create for this new socket object.
Create a CSocketFile object, associating the CSocket object with it.
Create a CArchive object for either loading (receiving) or storing (sending) data. The archive is associated with the CSocketFile object.
Keep in mind that CArchive doesn’t work with datagram sockets.
Use the CArchive object to pass data between the client and server sockets.
Keep in mind that a given CArchive object moves data in one direction only: either for loading (receiving) or storing (sending). In some cases, you’ll use two CArchive objects, one for sending data, the other for receiving acknowledgments.
After accepting a connection and setting up the archive, you can perform such tasks as validating passwords.
Destroy the archive, socket file, and socket objects.
Note Class CArchive supplies the IsBufferEmpty member function specifically for use with class CSocket. If the buffer contains multiple data messages, for example, you need to loop until all of them are read and the buffer is cleared. Otherwise, your next notification that there is data to be received may be indefinitely delayed. Use IsBufferEmpty to assure that you retrieve all data. For examples of using IsBufferEmpty, see the CHATSRVR sample application. For source code and information about MFC samples, see MFC Samples.
The article Windows Sockets: Sequence of Operations illustrates both sides of this process with example code.
What do you want to know more about?
Windows Sockets: Stream Sockets
Windows Sockets: Datagram Sockets
See Also CSocket::Create
Home | Overview | How Do I | Sample
This article describes the CSocket programming model. Class CSocket supplies socket support at a higher level of abstraction than does class CAsyncSocket. CSocket uses a version of the MFC serialization protocol to pass data to and from a socket object via an MFC CArchive object. CSocket provides blocking (while managing background processing of Windows messages) and gives you access to CArchive, which manages many aspects of the communication that you would have to do yourself using either the raw API or class CAsyncSocket.
Tip You can use class CSocket by itself, as a more convenient version of CAsyncSocket, but the simplest programming model is to use CSocket with a CArchive object.
For additional information about how the implementation of sockets with archives works, see the article Windows Sockets: How Sockets with Archives Work. For example code, see the articles Windows Sockets: Sequence of Operations and Windows Sockets: Example of Sockets Using Archives. For information about some of the functionality you can gain by deriving your own classes from the sockets classes, see the article Windows Sockets: Deriving from Socket Classes.
Caution If you are writing an MFC client program to communicate with established (non-MFC) servers, don’t send C++ objects via the archive. Unless the server is an MFC application that understands the kinds of objects you want to send, it won’t be able to receive and deserialize your objects. For related material on the subject of communicating with non-MFC applications, also see the article Windows Sockets: Byte Ordering.
The CSocket Programming Model
Using a CSocket object involves creating and associating together several MFC class objects. In the general procedure below, each step is taken by both the server socket and the client socket, except for step 3, in which each socket type requires a different action.
Tip At run time, the server application usually starts first in order to be ready and “listening” when the client application seeks a connection. If the server is not ready when the client tries to connect, you typically require the user application to try connecting again later.
To set up communication between a server socket and a client socket
Construct a CSocket object.
Use the object to create the underlying SOCKET handle.
For a CSocket client object, you should normally use the default parameters to Create, unless you need a datagram socket. For a CSocket server object, you must specify a port in the Create call.
Note CArchive doesn’t work with datagram sockets. If you want to use CSocket for a datagram socket, you must use the class as you would CAsyncSocket — without an archive. Because datagrams are unreliable (not guaranteed to arrive and may be repeated or out of sequence), they aren’t compatible with serialization via an archive. You expect a serialization operation to complete reliably and in sequence. If you try to use CSocket with a CArchive object for a datagram, an MFC assertion fails.
If the socket is a client, call CAsyncSocket::Connect to connect the socket object to a server socket.
-or-
If the socket is a server, call CAsyncSocket::Listen to begin listening for connect attempts from a client. Upon receiving a connection request, accept it by calling CAsyncSocket::Accept.
Note The Accept member function takes a reference to a new, empty CSocket object as its parameter. You must construct this object before you call Accept. Keep in mind that if this socket object goes out of scope, the connection closes. Do not call Create for this new socket object.
Create a CSocketFile object, associating the CSocket object with it.
Create a CArchive object for either loading (receiving) or storing (sending) data. The archive is associated with the CSocketFile object.
Keep in mind that CArchive doesn’t work with datagram sockets.
Use the CArchive object to pass data between the client and server sockets.
Keep in mind that a given CArchive object moves data in one direction only: either for loading (receiving) or storing (sending). In some cases, you’ll use two CArchive objects, one for sending data, the other for receiving acknowledgments.
After accepting a connection and setting up the archive, you can perform such tasks as validating passwords.
Destroy the archive, socket file, and socket objects.
Note Class CArchive supplies the IsBufferEmpty member function specifically for use with class CSocket. If the buffer contains multiple data messages, for example, you need to loop until all of them are read and the buffer is cleared. Otherwise, your next notification that there is data to be received may be indefinitely delayed. Use IsBufferEmpty to assure that you retrieve all data. For examples of using IsBufferEmpty, see the CHATSRVR sample application. For source code and information about MFC samples, see MFC Samples.
The article Windows Sockets: Sequence of Operations illustrates both sides of this process with example code.
What do you want to know more about?
Windows Sockets: Stream Sockets
Windows Sockets: Datagram Sockets
See Also CSocket::Create
#7
void SocketThreadInit()
{
#ifndef _AFXDLL
#define _AFX_SOCK_THREAD_STATE AFX_MODULE_THREAD_STATE
#define _afxSockThreadState AfxGetModuleThreadState()
_AFX_SOCK_THREAD_STATE* pState = _afxSockThreadState;
if (pState-> m_pmapSocketHandle == NULL)
pState-> m_pmapSocketHandle = new CMapPtrToPtr;
if (pState-> m_pmapDeadSockets == NULL)
pState-> m_pmapDeadSockets = new CMapPtrToPtr;
if (pState-> m_plistSocketNotifications == NULL)
pState-> m_plistSocketNotifications = new CPtrList;
#endif
}
每次进行Socket.Create() 之前执行下这段代码,将会避免出错,
结贴
{
#ifndef _AFXDLL
#define _AFX_SOCK_THREAD_STATE AFX_MODULE_THREAD_STATE
#define _afxSockThreadState AfxGetModuleThreadState()
_AFX_SOCK_THREAD_STATE* pState = _afxSockThreadState;
if (pState-> m_pmapSocketHandle == NULL)
pState-> m_pmapSocketHandle = new CMapPtrToPtr;
if (pState-> m_pmapDeadSockets == NULL)
pState-> m_pmapDeadSockets = new CMapPtrToPtr;
if (pState-> m_plistSocketNotifications == NULL)
pState-> m_plistSocketNotifications = new CPtrList;
#endif
}
每次进行Socket.Create() 之前执行下这段代码,将会避免出错,
结贴
#1
当文件已存在时,无法创建该文件。
#2
错误的意思我知道,但是为什么有这种错误,我该怎么避开
#3
#4
我自己顶下,求帮助
#5
要么析构之前的CSocket之后再构造新的CSocket,再CSocket.Create()
要么使用另一个CSocket
要么使用CSocket数组
要么使用另一个CSocket
要么使用CSocket数组
#6
Windows Sockets: Using Sockets with Archives
Home | Overview | How Do I | Sample
This article describes the CSocket programming model. Class CSocket supplies socket support at a higher level of abstraction than does class CAsyncSocket. CSocket uses a version of the MFC serialization protocol to pass data to and from a socket object via an MFC CArchive object. CSocket provides blocking (while managing background processing of Windows messages) and gives you access to CArchive, which manages many aspects of the communication that you would have to do yourself using either the raw API or class CAsyncSocket.
Tip You can use class CSocket by itself, as a more convenient version of CAsyncSocket, but the simplest programming model is to use CSocket with a CArchive object.
For additional information about how the implementation of sockets with archives works, see the article Windows Sockets: How Sockets with Archives Work. For example code, see the articles Windows Sockets: Sequence of Operations and Windows Sockets: Example of Sockets Using Archives. For information about some of the functionality you can gain by deriving your own classes from the sockets classes, see the article Windows Sockets: Deriving from Socket Classes.
Caution If you are writing an MFC client program to communicate with established (non-MFC) servers, don’t send C++ objects via the archive. Unless the server is an MFC application that understands the kinds of objects you want to send, it won’t be able to receive and deserialize your objects. For related material on the subject of communicating with non-MFC applications, also see the article Windows Sockets: Byte Ordering.
The CSocket Programming Model
Using a CSocket object involves creating and associating together several MFC class objects. In the general procedure below, each step is taken by both the server socket and the client socket, except for step 3, in which each socket type requires a different action.
Tip At run time, the server application usually starts first in order to be ready and “listening” when the client application seeks a connection. If the server is not ready when the client tries to connect, you typically require the user application to try connecting again later.
To set up communication between a server socket and a client socket
Construct a CSocket object.
Use the object to create the underlying SOCKET handle.
For a CSocket client object, you should normally use the default parameters to Create, unless you need a datagram socket. For a CSocket server object, you must specify a port in the Create call.
Note CArchive doesn’t work with datagram sockets. If you want to use CSocket for a datagram socket, you must use the class as you would CAsyncSocket — without an archive. Because datagrams are unreliable (not guaranteed to arrive and may be repeated or out of sequence), they aren’t compatible with serialization via an archive. You expect a serialization operation to complete reliably and in sequence. If you try to use CSocket with a CArchive object for a datagram, an MFC assertion fails.
If the socket is a client, call CAsyncSocket::Connect to connect the socket object to a server socket.
-or-
If the socket is a server, call CAsyncSocket::Listen to begin listening for connect attempts from a client. Upon receiving a connection request, accept it by calling CAsyncSocket::Accept.
Note The Accept member function takes a reference to a new, empty CSocket object as its parameter. You must construct this object before you call Accept. Keep in mind that if this socket object goes out of scope, the connection closes. Do not call Create for this new socket object.
Create a CSocketFile object, associating the CSocket object with it.
Create a CArchive object for either loading (receiving) or storing (sending) data. The archive is associated with the CSocketFile object.
Keep in mind that CArchive doesn’t work with datagram sockets.
Use the CArchive object to pass data between the client and server sockets.
Keep in mind that a given CArchive object moves data in one direction only: either for loading (receiving) or storing (sending). In some cases, you’ll use two CArchive objects, one for sending data, the other for receiving acknowledgments.
After accepting a connection and setting up the archive, you can perform such tasks as validating passwords.
Destroy the archive, socket file, and socket objects.
Note Class CArchive supplies the IsBufferEmpty member function specifically for use with class CSocket. If the buffer contains multiple data messages, for example, you need to loop until all of them are read and the buffer is cleared. Otherwise, your next notification that there is data to be received may be indefinitely delayed. Use IsBufferEmpty to assure that you retrieve all data. For examples of using IsBufferEmpty, see the CHATSRVR sample application. For source code and information about MFC samples, see MFC Samples.
The article Windows Sockets: Sequence of Operations illustrates both sides of this process with example code.
What do you want to know more about?
Windows Sockets: Stream Sockets
Windows Sockets: Datagram Sockets
See Also CSocket::Create
Home | Overview | How Do I | Sample
This article describes the CSocket programming model. Class CSocket supplies socket support at a higher level of abstraction than does class CAsyncSocket. CSocket uses a version of the MFC serialization protocol to pass data to and from a socket object via an MFC CArchive object. CSocket provides blocking (while managing background processing of Windows messages) and gives you access to CArchive, which manages many aspects of the communication that you would have to do yourself using either the raw API or class CAsyncSocket.
Tip You can use class CSocket by itself, as a more convenient version of CAsyncSocket, but the simplest programming model is to use CSocket with a CArchive object.
For additional information about how the implementation of sockets with archives works, see the article Windows Sockets: How Sockets with Archives Work. For example code, see the articles Windows Sockets: Sequence of Operations and Windows Sockets: Example of Sockets Using Archives. For information about some of the functionality you can gain by deriving your own classes from the sockets classes, see the article Windows Sockets: Deriving from Socket Classes.
Caution If you are writing an MFC client program to communicate with established (non-MFC) servers, don’t send C++ objects via the archive. Unless the server is an MFC application that understands the kinds of objects you want to send, it won’t be able to receive and deserialize your objects. For related material on the subject of communicating with non-MFC applications, also see the article Windows Sockets: Byte Ordering.
The CSocket Programming Model
Using a CSocket object involves creating and associating together several MFC class objects. In the general procedure below, each step is taken by both the server socket and the client socket, except for step 3, in which each socket type requires a different action.
Tip At run time, the server application usually starts first in order to be ready and “listening” when the client application seeks a connection. If the server is not ready when the client tries to connect, you typically require the user application to try connecting again later.
To set up communication between a server socket and a client socket
Construct a CSocket object.
Use the object to create the underlying SOCKET handle.
For a CSocket client object, you should normally use the default parameters to Create, unless you need a datagram socket. For a CSocket server object, you must specify a port in the Create call.
Note CArchive doesn’t work with datagram sockets. If you want to use CSocket for a datagram socket, you must use the class as you would CAsyncSocket — without an archive. Because datagrams are unreliable (not guaranteed to arrive and may be repeated or out of sequence), they aren’t compatible with serialization via an archive. You expect a serialization operation to complete reliably and in sequence. If you try to use CSocket with a CArchive object for a datagram, an MFC assertion fails.
If the socket is a client, call CAsyncSocket::Connect to connect the socket object to a server socket.
-or-
If the socket is a server, call CAsyncSocket::Listen to begin listening for connect attempts from a client. Upon receiving a connection request, accept it by calling CAsyncSocket::Accept.
Note The Accept member function takes a reference to a new, empty CSocket object as its parameter. You must construct this object before you call Accept. Keep in mind that if this socket object goes out of scope, the connection closes. Do not call Create for this new socket object.
Create a CSocketFile object, associating the CSocket object with it.
Create a CArchive object for either loading (receiving) or storing (sending) data. The archive is associated with the CSocketFile object.
Keep in mind that CArchive doesn’t work with datagram sockets.
Use the CArchive object to pass data between the client and server sockets.
Keep in mind that a given CArchive object moves data in one direction only: either for loading (receiving) or storing (sending). In some cases, you’ll use two CArchive objects, one for sending data, the other for receiving acknowledgments.
After accepting a connection and setting up the archive, you can perform such tasks as validating passwords.
Destroy the archive, socket file, and socket objects.
Note Class CArchive supplies the IsBufferEmpty member function specifically for use with class CSocket. If the buffer contains multiple data messages, for example, you need to loop until all of them are read and the buffer is cleared. Otherwise, your next notification that there is data to be received may be indefinitely delayed. Use IsBufferEmpty to assure that you retrieve all data. For examples of using IsBufferEmpty, see the CHATSRVR sample application. For source code and information about MFC samples, see MFC Samples.
The article Windows Sockets: Sequence of Operations illustrates both sides of this process with example code.
What do you want to know more about?
Windows Sockets: Stream Sockets
Windows Sockets: Datagram Sockets
See Also CSocket::Create
#7
void SocketThreadInit()
{
#ifndef _AFXDLL
#define _AFX_SOCK_THREAD_STATE AFX_MODULE_THREAD_STATE
#define _afxSockThreadState AfxGetModuleThreadState()
_AFX_SOCK_THREAD_STATE* pState = _afxSockThreadState;
if (pState-> m_pmapSocketHandle == NULL)
pState-> m_pmapSocketHandle = new CMapPtrToPtr;
if (pState-> m_pmapDeadSockets == NULL)
pState-> m_pmapDeadSockets = new CMapPtrToPtr;
if (pState-> m_plistSocketNotifications == NULL)
pState-> m_plistSocketNotifications = new CPtrList;
#endif
}
每次进行Socket.Create() 之前执行下这段代码,将会避免出错,
结贴
{
#ifndef _AFXDLL
#define _AFX_SOCK_THREAD_STATE AFX_MODULE_THREAD_STATE
#define _afxSockThreadState AfxGetModuleThreadState()
_AFX_SOCK_THREAD_STATE* pState = _afxSockThreadState;
if (pState-> m_pmapSocketHandle == NULL)
pState-> m_pmapSocketHandle = new CMapPtrToPtr;
if (pState-> m_pmapDeadSockets == NULL)
pState-> m_pmapDeadSockets = new CMapPtrToPtr;
if (pState-> m_plistSocketNotifications == NULL)
pState-> m_plistSocketNotifications = new CPtrList;
#endif
}
每次进行Socket.Create() 之前执行下这段代码,将会避免出错,
结贴