資源簡介
本程序使用了Socket,多線程等技術,功能是在控制臺中實現多機聊天的功能,環境為windows+vs2013

代碼片段和文件信息
//?QQ_client.cpp?:?定義控制臺應用程序的入口點。
//
#include?“stdafx.h“
#include
#include
#include
#include
#pragma?comment(lib?“ws2_32.lib“)?
using?namespace?std;
WSADATA?wsd;
//發送數據遠程服務器套接字
SOCKET?Sremotehost;
//發送數據遠程服務器地址
SOCKADDR_IN?addrRemoteServ;
int?addrRemoteServLen;
//批大小
const?int?BUFF_SIZE?=?1024;
int?Init_Chat(char?ip[]?char?port[])//char?ip[]char?port[]
{
//初始化套接字庫
if?(WSAStartup(MAKEWORD(2?2)?&wsd)?!=?0)
{
cout?<“WSAStartup?failed“?< return?-1;
}
cout?<“Chat?Socket?Init...“?< Sremotehost?=?socket(AF_INET?SOCK_STREAM?IPPROTO_TCP);
if?(Sremotehost?==?INVALID_SOCKET)
{
cout?<“Chat?Socket?Failed“?< WSACleanup();
return?-1;
}
cout?<“Service?Socket?Done...“?< addrRemoteServ.sin_family?=?AF_INET;
addrRemoteServ.sin_port?=?htons((short)atoi(port));//4999atoi(port)
addrRemoteServ.sin_addr.s_addr?=?inet_addr(ip);//“127.0.0.1“
addrRemoteServLen?=?sizeof(addrRemoteServ);
//連接服務器
int?ret?=?connect(Sremotehost?(LPSOCKADDR)&addrRemoteServ?addrRemoteServLen);
if?(ret?==?SOCKET_ERROR)
{
cout?<“Connect?Failed“?< cout?< closesocket(Sremotehost);
WSACleanup();
return?-1;
}
cout?<“Connect?Done...“?< return?0;
}
//向服務器發送數據
int?Send_Info(char?info[])
{
char?recv_buf[BUFF_SIZE];
//cout?<“Send?to?service:“?< int?ret?=?send(Sremotehost?info?BUFF_SIZE?0);
if?(ret?==?SOCKET_ERROR)
{
cout?<“Send?failed“?< }
ZeroMemory(recv_buf?BUFF_SIZE);//如果不初始化,后面全都是??燙燙燙燙
recv(Sremotehost?recv_buf?BUFF_SIZE?0);
//cout?< cout?<“Sending...?\n“?< cout?<“\n“?< //getpeername(Sremotehost?(struct?sockaddr?*)&addrRemoteServ?&addrRemoteServLen);?//獲取connfd表示的連接上的對端地址
//cout?<“To?Friend:?“?< cout?<“\n“?< return?0;
}
//聊天
int?chat()
{
char?ip[16]?port[6];
cout?<“Input?his?ip“?< cin?>>?ip;
cout?<“Input?his?port“?< cin?>>?port;
int?ret?=?Init_Chat(ip?port);
if?(ret?==?SOCKET_ERROR)
{
cout?<“Init?Chat?Failed“?< return?-1;
}
//Init();
char?info[BUFF_SIZE];
cout?<“Enjoy?your?chatting...“?< cin.get();
cout?<“\n“;
while?(true)
{
cout?<“Please?Insert?Your?Word:(If?you?want?to?cancel?this?chatPlease?input?quit)“;
ZeroMemory(info?BUFF_SIZE);
cin.getline(info?1024?‘\n‘);
cout?<“You:“?< if?(strcmp(info?“quit“)?==?0)
{
cout?<“Quiting...“?< break;
}
Send_Info(info);
}
}
WSADATA?wsd2;
//接收消息服務器套接字
SOCKET?Sserver;
//接收消息服務器套接字地址
SOCKADDR_IN?addrServ;
//接收消息發送方遠程客戶端套接字
SOCKET?Sclient;
//接收消息發送方遠程客戶端套接字地址
sockaddr_in?addrClient;
//接收消息發送方遠程客戶端套接字地址長度
int?addrClientLen;
in
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????88576??2018-05-07?10:12??SocketDemo\Debug\SocketDemo.exe
?????文件?????577504??2018-05-07?10:12??SocketDemo\Debug\SocketDemo.ilk
?????文件????1748992??2018-05-07?10:12??SocketDemo\Debug\SocketDemo.pdb
?????文件????3080192??2018-05-05?15:55??SocketDemo\ipch\socketdemo-195ec9d4\socketdemo-21b5067a.ipch
?????文件????????407??2018-05-07?10:27??SocketDemo\README.txt
?????文件???????1601??2018-05-07?10:12??SocketDemo\SocketDemo\Debug\SocketDemo.log
?????文件?????280861??2018-05-07?10:12??SocketDemo\SocketDemo\Debug\SocketDemo.obj
?????文件????1703936??2018-05-05?15:55??SocketDemo\SocketDemo\Debug\SocketDemo.pch
?????文件???????1454??2018-05-07?10:12??SocketDemo\SocketDemo\Debug\SocketDemo.tlog\cl.command.1.tlog
?????文件??????33298??2018-05-07?10:12??SocketDemo\SocketDemo\Debug\SocketDemo.tlog\CL.read.1.tlog
?????文件???????7600??2018-05-07?09:49??SocketDemo\SocketDemo\Debug\SocketDemo.tlog\CL.read.4.tlog
?????文件??????21122??2018-05-07?10:12??SocketDemo\SocketDemo\Debug\SocketDemo.tlog\CL.read.5.tlog
?????文件???????1042??2018-05-07?10:12??SocketDemo\SocketDemo\Debug\SocketDemo.tlog\CL.write.1.tlog
?????文件???????1284??2018-05-07?10:12??SocketDemo\SocketDemo\Debug\SocketDemo.tlog\li
?????文件???????3612??2018-05-07?10:12??SocketDemo\SocketDemo\Debug\SocketDemo.tlog\li
?????文件????????548??2018-05-07?10:12??SocketDemo\SocketDemo\Debug\SocketDemo.tlog\li
?????文件????????162??2018-05-07?10:12??SocketDemo\SocketDemo\Debug\SocketDemo.tlog\SocketDemo.lastbuildstate
?????文件??????11474??2018-05-05?15:55??SocketDemo\SocketDemo\Debug\stdafx.obj
?????文件?????961536??2018-05-07?10:12??SocketDemo\SocketDemo\Debug\vc120.idb
?????文件?????577536??2018-05-07?10:12??SocketDemo\SocketDemo\Debug\vc120.pdb
?????文件???????1531??2018-05-05?15:55??SocketDemo\SocketDemo\ReadMe.txt
?????文件???????6703??2018-05-07?10:21??SocketDemo\SocketDemo\SocketDemo.cpp
?????文件???????4539??2018-05-05?15:55??SocketDemo\SocketDemo\SocketDemo.vcxproj
?????文件???????1317??2018-05-05?15:55??SocketDemo\SocketDemo\SocketDemo.vcxproj.filters
?????文件????????216??2018-05-05?15:55??SocketDemo\SocketDemo\stdafx.cpp
?????文件????????234??2018-05-05?15:55??SocketDemo\SocketDemo\stdafx.h
?????文件????????236??2018-05-05?15:55??SocketDemo\SocketDemo\targetver.h
?????文件???37224448??2018-05-07?10:21??SocketDemo\SocketDemo.sdf
?????文件????????976??2018-05-05?15:55??SocketDemo\SocketDemo.sln
????..A..H.?????19456??2018-05-07?10:21??SocketDemo\SocketDemo.v12.suo
............此處省略10個文件信息
- 上一篇:DevC++ 4.9
- 下一篇:UG二次開發用戶界面(MFC簡單)
評論
共有 條評論