1 Đồ án socket.....help...help..help... 2010-06-13, 19:12
nhox_IT
Thành Viên
Sau khi link đồ án socket đóng. mí bạn cao thủ làm được những đề nào có thể post lên cho pà kon tham khảo với nhak...hihi...thanks mọi người nhìu nhìu...
Lên Google search xem có bài mẫu nào không . Dĩ nhiên bài do tự mình làm ra dù code tốt hay xấu cũng đáng tự hàoMrSku đã viết:
Một mình làm nên cùi lắm nhưng vẫn vui vì do chính tay mình làm ra mà ko copy của ai ha ha
Hiz lên tìm mà ko ra toàn là bên java php với C# còn C++ thì hok có...suy nghĩ cả tuần ko ra đến cái ngày phải đi học ở trung tâm thì tìm ra chân lý nên phải bỏ một buổi học ở trung tâm để làm em nó đó >"<boy_saudoi đã viết:Lên Google search xem có bài mẫu nào không . Dĩ nhiên bài do tự mình làm ra dù code tốt hay xấu cũng đáng tự hàoMrSku đã viết:
Một mình làm nên cùi lắm nhưng vẫn vui vì do chính tay mình làm ra mà ko copy của ai ha ha
Phiền phức quá nhĩ,muốn xem code của bác Admin mà khó quá Thui chịu khó post trực tiếp điMrSku đã viết:Ai muốn down thì pm mình lấy pass nha
Do thói quen up media là có pass
// Client.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include "Client.h"
#include<string.h>
#include<conio.h>
#include<afxsock.h>
#include<stdio.h>
#ifndef _MauSac_
#define _MauSac_
void MauSac(int x)
{
HANDLE mau;
mau = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(mau,x);
}
#endif
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
CWinApp theApp;
typedef struct{
int x;
int y;
}DATA;
int k=0;
void menu()
{
printf("\nBam Phim 1 --> Gieo Xuc Xac");
printf("\nBam: ");
}
void duongdua(int a[][100])
{
for (int i=0;i<15;i++)
{
a[0][i]=i;
}
for (i=1;i<15;i++)
{
a[1][i]=-1;
}
a[1][k]=-2;
}
void xuat(int a[][100])
{
for(int i=0;i<2;i++)
{
for(int j=0;j<15;j++)
{
if(a[i][j]==-2)
{
MauSac(10);
printf("*\t");
MauSac(7);
}
else if(a[i][j]==-1)
{
printf("||\t");
}
else
{
printf("%d\t",a[i][j]);
}
}
printf("\n");
}
}
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
AfxSocketInit(NULL);
CSocket socketConnect;
socketConnect.Create();
char ipserver[10];
char choi[100];
printf("Nhap vao ip cua Server: ");
gets(ipserver);
int loi = socketConnect.Connect(ipserver, 12345);
if(loi == 0)
{
printf("Loi ket noi den Server\n");
return 0;
}
int kqtrung;
char nickname[10];
do
{
kqtrung = 0;
printf("Nhap vao nickname dang ky voi server: ");
scanf("%s", nickname);
socketConnect.Send(nickname, sizeof(nickname), 0);
socketConnect.Receive(&kqtrung, sizeof(kqtrung), 0);
if (kqtrung == 1)
{
printf("Nick name bi trung.\n");
}
}
while (kqtrung == 1);
socketConnect.Receive((void*)choi,100);
cout<<choi;
int a[2][100];
int b[2][100];
duongdua(a);
duongdua(b);
char luachon[5];
int flag=1;
int songaunhien;
int batdau=1;
int l=0;
int v=0;
while(batdau==1)
{
printf("\nVi Tri Hien Tai Cua Ban: \n");
xuat(a);
printf("\nVi Tri Hien Tai Doi Thu: \n");
xuat(b);
a[1][l]=-1;
b[1][v]=-1;
char NguoiThang[10];
socketConnect.Receive((void*)&v,sizeof(int));
socketConnect.Receive((void*)&batdau,sizeof(int));
if(batdau==0)
{
printf("\nThua Cuoc . Game Over");
socketConnect.Receive((void*)&NguoiThang,sizeof(int));
printf("\n\nDoi Thu Da Toi Dich \n\n\n");
a[1][14]=-2;
printf("\n");
xuat(a);
printf("\nNguoi Choi Chien Thang: %s \n",NguoiThang);
getch();
return 0;
}
else
{
tt:menu();
cin >> luachon;
if(stricmp(luachon,"1")==0)
{
socketConnect.Send((void*)luachon,5);
socketConnect.Receive((void*)&songaunhien,sizeof(int));
socketConnect.Receive((void*)&l,sizeof(int));
if(l!=14)
{
cout << "\nSo Diem Nhan Duoc: "<<songaunhien;
a[1][l]=-2;
b[1][v]=-2;
}
else
{
a[1][l]=-2;
b[1][v]=-2;
cout << "\nSo Diem Nhan Duoc: "<<songaunhien;
printf("\n\nVictory !!! Chien Thang . Game Over");
printf("\n\n\n");
xuat(a);
break;
}
}
else
{
printf("\nNhap Sai . Gieo Xuc Xac --> Bam phim 1 ");
goto tt;
}
}
}
socketConnect.Close();
getch();
}
return nRetCode;
}
// SerVer.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "SerVer.h"
#include<string.h>
#include<conio.h>
#include<afxsock.h>
#include<time.h>
#include<stdlib.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
CWinApp theApp;
typedef struct{
int x;
int y;
}DATA;
struct NickName
{
char tenclient[10];
};
typedef struct NickName NICKNAME;
struct vitri
{
int l;
};
typedef struct vitri VT;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
int n=2;
int max=14;
//initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
AfxSocketInit(NULL);
CSocket socketListen, *arrsocketAccept;//arrsocketAccept[3];
arrsocketAccept = new CSocket[3];
socketListen.Create(12345);
socketListen.Listen();
for (int i = 0; i < 2; i ++)
{
printf("Server cho ket noi Client thu: %d\n", i + 1);
socketListen.Accept(arrsocketAccept[i]);
}
NICKNAME *nickName;
nickName = new NICKNAME[2];
for (i = 0; i < 2; i ++)
{
int kqtrung;
do
{
kqtrung = 0;
printf("Server cho nhan nick name tu Client thu: %d\n", i + 1);
arrsocketAccept[i].Receive(&nickName[i], sizeof(nickName[i]), 0);
printf("NickName Server nhan duoc la: %s\n", nickName[i].tenclient);
for(int j = 0; j < i; j ++)
{
if(strcmp(nickName[j].tenclient, nickName[i].tenclient) == 0)
{
kqtrung = 1;
break;
}
}
arrsocketAccept[i].Send(&kqtrung, sizeof(kqtrung), 0);
} while (kqtrung == 1);
}
char choi[100]="Bat Dau Choi";
char sai[100]="Nhap Sai";
for (i = 0; i < 2; i++)
{
arrsocketAccept[i].Send((void*)choi,100);
}
char lc[2];
int bd=1;
int a,b,so;
VT k[2];
k[0].l=0;
k[1].l=0;
int temp=0;
arrsocketAccept[0].Send((void*)&temp,sizeof(int));
while((k[0].l!=14 && k[1].l!=14))
{
for (i = 0; i < 2; i++)
{
arrsocketAccept[i].Send((void*)&bd,sizeof(int));
printf("Server nhan duoc yeu cau Gieo Xuc Xac tu Client thu: %d\n", i + 1);
arrsocketAccept[i].Receive((void*)lc,5);
a=1,b=5;
srand((unsigned int)time(NULL));
so = rand()%(b-a) + a;
arrsocketAccept[i].Send((void*)&so,sizeof(int));
k[i].l=k[i].l+so;
if(k[i].l<max)
{
arrsocketAccept[i].Send((void*)&k[i].l,sizeof(int));
arrsocketAccept[n-i-1].Send((void*)&k[i].l,sizeof(int));
}
else if(k[i].l>max)
{
k[i].l=k[i].l-so;
arrsocketAccept[i].Send((void*)&k[i].l,sizeof(int));
arrsocketAccept[n-i-1].Send((void*)&k[n-i-1].l,sizeof(int));
}
else
{
k[i].l=14;
arrsocketAccept[i].Send((void*)&k[i].l,sizeof(int));
arrsocketAccept[n-i-1].Send((void*)&k[i].l,sizeof(int));
bd=0;
arrsocketAccept[n-i-1].Send((void*)&bd,sizeof(int));
arrsocketAccept[n-i-1].Send(&nickName[i],sizeof(nickName[i]));
break;
}
}
}
for (i = 0; i < 2; i++)
{
arrsocketAccept[i].Close();
}
}
return nRetCode;
}
Permissions in this forum:
Bạn không có quyền trả lời bài viết
|
|