Tạo chương trình chat đơn giản, mình đã biên dịc thành công Client và server đều được mà sao khi chạy Client yêu cầu nhập địa chỉ Ip máy chủ mặc định: 127.0.0.1 (IP mặc định của server Khi Client và server cùng chạy trên 1 máy duy nhất) thì lại báo lỗi: "Error Connecting Server" , mong các bạn chỉ ra nguyên nhân giúp mình.
Mình dùng visio 2010!
Sau đây là đoạn Code của 2 chương trình client và server:
Name Project: server.cpp
Code:
// server.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "server.h"
#include "afxsock.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// The one and only application object
CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
HMODULE hModule = ::GetModuleHandle(NULL);
if (hModule != NULL)
{
// initialize MFC and print and error on failure
if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
AfxSocketInit(NULL);
CSocket socketListen, socketAccept, socketConnect;
char sMsg[4096];
int nMsgLen;
printf("dang cho ket noi...\n");
if (!socketListen.Create(12345) || !socketListen.Listen() || !socketListen.Accept(socketAccept))
{
printf(" Loi! Khong the ket noi...\n");
exit(0);
}
do
{
printf(" Da ket noi thanh cong...\n");
socketAccept.Receive(&nMsgLen, sizeof(nMsgLen));
socketAccept.Receive(sMsg, nMsgLen);
sMsg[nMsgLen] = 0; // end of string
printf(">%s\n", sMsg);
if (strcmpi(sMsg, "exit") == 0)
break;
printf("Msg>");
gets(sMsg);
nMsgLen = strlen(sMsg);
socketConnect.Send(&nMsgLen, sizeof(nMsgLen));
socketConnect.Send(sMsg, nMsgLen);
}
while (strcmpi(sMsg, "exit") != 0);
socketAccept.Close();
socketListen.Close();
socketConnect.Close();
}
}
else
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: GetModuleHandle failed\n"));
nRetCode = 1;
}
return nRetCode;
}
Name Project: Client.cpp
Code:
// Client.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Client.h"
#include "afxsock.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// The one and only application object
CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
HMODULE hModule = ::GetModuleHandle(NULL);
if (hModule != NULL)
{
// initialize MFC and print and error on failure
if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
AfxSocketInit(NULL);
CSocket socketConnect, socketAccept;
char sMsg[4096], sServerAddress[1024];
int nMsgLen;
printf("ServerAddress : ");
gets(sServerAddress);
if (!socketConnect.Create() ||
!socketConnect.Connect((LPCTSTR)sServerAddress, (UINT)12345))
{
printf(" Loi! khong the ket noi server...!");
exit(0);
}
do
{
printf("Msg>");
gets(sMsg);
nMsgLen = strlen(sMsg);
socketConnect.Send(&nMsgLen, sizeof(nMsgLen));
socketConnect.Send(sMsg, nMsgLen);
if (strcmpi(sMsg, "exit") == 0)
break;
socketAccept.Receive(&nMsgLen, sizeof(nMsgLen));
socketAccept.Receive(sMsg, nMsgLen);
sMsg[nMsgLen] = 0; // end of string
printf(">%s\n", sMsg);
}
while (strcmpi(sMsg, "exit") != 0);
socketConnect.Close();
socketAccept.Close();
}
}
else
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: GetModuleHandle failed\n"));
nRetCode = 1;
}
return nRetCode;
}
Ảnh trang thái server sau khi biên dịch đang nghe kết nối từ client:

Ảnh trang thái Client sau khi biên dịch thành công đang chờ người dùng nhập địa chỉ IP máy chủ để kết nối:

Đang nhập IP server để kết nối:

Sau khi xác nhận IP server và báo lỗi :

Mấy sư huynh giúp mình với nha...!
Tks!