1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
//////////Create a socket////////////////////////
//Create a SOCKET object called m_socket.
SOCKET m_socket;
// Call the socket function and return its value to the m_socket variable.
// For this application, use the Internet address family, streaming sockets, and
// the TCP/IP protocol.
// using AF_INET family, TCP socket type and protocol of the AF_INET - IPv4
m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
// Check for errors to ensure that the socket is a valid socket.
if (m_socket == INVALID_SOCKET)
{
MessageBox (NULL, TEXT("INVALID_SOCKET"), TEXT("INVALID_SOCKET"), MB_OK);
WSACleanup();
return 0;
}
else
{
MessageBox (NULL, TEXT("Socket OK"), TEXT("Socket OK"), MB_OK);
}
////////////////bind()//////////////////////////////
// Create a sockaddr_in object and set its values.
sockaddr_in service;
// AF_INET is the Internet address family.
service.sin_family = AF_INET;
// "127.0.0.1" is the local IP address to which the socket will be bound.
service.sin_addr.s_addr = inet_addr(HOSTNAME);
// 21 is the port number to which the socket will be bound.
service.sin_port = htons(PORTNUM);
// Call the bind function, passing the created socket and the sockaddr_in structure as parameters.
// Check for general errors.
if (bind(m_socket, (SOCKADDR*)&service, sizeof(service)) == SOCKET_ERROR)
{
closesocket(m_socket);
MessageBox (NULL, TEXT("SOCKET_ERROR"), TEXT("SOCKET_ERROR"), MB_OK);
return 0;
}
else
{
char temp[256];
int num=send(m_socket,temp,255,MSG_DONTROUTE);
if(num<0)
MessageBox (NULL, TEXT("num"),TEXT("<0"), MB_OK);
else
MessageBox (NULL, TEXT("Byte writen"),TEXT(">=0"), MB_OK);
} |
Partager