Ⅰ 在C或C++中,怎么把俩个程序连在一起
可以把程序1作为头文件(program_1.h),在程序2中声明一下就可以了(include"program_1.h"),举一个回简单的例子吧:
程序答1(program_1.h):
void print(){
cout<<"Hello World!\n";
}
程序2(program_2.cpp):
include<iostream.h>
include"program_1.h"
void main(){
print();
}
Ⅱ C++ 两个程序之间怎么才能建立数据连接
这要用到网络编程 也就是socket编程
下面给出两个源代码一个是客户端,一个是服务端
他们可以进行连接通讯
服务端程序代码:
#include <stdio.h>
#include "scompat.h"
/* try to tell linker where WinSock library is */
#if defined(_MSC_VER)
# pragma comment(lib,"wsock32.lib")
#elif defined(__BORLANDC__)
# pragma comment(lib,"mswsock.lib")
#endif
#define WSA_ERROR(x) { printf("Error %d: %s\n", \
WSAGetLastError(), x); return 1; }
int ServerLoop(SOCKET sd_listen, int isMultiTasking);
main(int argc, char **argv)
{
SOCKET sd_listen;
int err;
u_short iPort;
struct sockaddr_in addr_srv;
struct hostent *ptrHost;
char *pszHost = "152.160.13.253";
iPort = (argc >= 2) ? atoi(argv[1]) : 9999;
InitSockets();
sd_listen = socket(PF_INET, SOCK_STREAM, 0);
if (sd_listen == INVALID_SOCKET) {
printf("Error: out of socket resources\n");
return 1;
}
if (atoi(pszHost)) {
u_long ip_addr = inet_addr(pszHost);
ptrHost = gethostbyaddr((char *)&ip_addr,
sizeof(u_long), AF_INET);
}
else
ptrHost = gethostbyname(pszHost);
if (!ptrHost)
WSA_ERROR("cannot resolve hostname")
addr_srv.sin_family = PF_INET;
addr_srv.sin_addr.s_addr = htonl(INADDR_ANY);
addr_srv.sin_port = htons(iPort);
err = bind(sd_listen, (const struct sockaddr *) &addr_srv,
sizeof(addr_srv));
if (err == INVALID_SOCKET)
WSA_ERROR("Error: unable to bind socket\n")
err = listen(sd_listen, SOMAXCONN);
if (err == INVALID_SOCKET)
WSA_ERROR("Error: listen failed\n")
ServerLoop(sd_listen, 1);
printf("Server is down\n");
WSACleanup();
return 0;
}
void ServeAClient(LPVOID lpv)
{
SOCKET sd_accept = (SOCKET) lpv;
const char *msg = "HELLO CLIENT";
char response[4096];
memset(response, 0, sizeof(response));
recv(sd_accept, response, sizeof(response), 0);
if (strcmp(response, "HELLO SERVER")) {
printf("Application: client not using expected "
"protocol %s\n", response);
}
else
send (sd_accept, msg, strlen(msg)+1, 0);
closesocket(sd_accept);
}
#define MAX_SERVED 3
int ServerLoop(SOCKET sd_listen, int isMultiTasking)
{
SOCKET sd_accept;
struct sockaddr_in addr_client;
int err, nSize;
int numServed = 0;
HANDLE handles[MAX_SERVED];
int myID;
while (numServed < MAX_SERVED) {
nSize = sizeof(addr_client);
sd_accept = accept(sd_listen, (struct sockaddr *)
&addr_client, &nSize);
if (sd_accept == INVALID_SOCKET)
WSA_ERROR("Error: accept failed\n")
printf("Accepted connection from client at %s\n",
inet_ntoa(addr_client.sin_addr));
if (isMultiTasking) {
#ifdef _WIN32
handles[numServed] = CreateThread(NULL, 1000,
(LPTHREAD_START_ROUTINE)ServeAClient,
(LPVOID) sd_accept, 0, &myID);
#else
myID = fork();
if (myID == 0) { /* I am child process */
ServeAClient ((LPVOID) sd_accept);
exit(0);
}
handles[numServed] = myID;
#endif
}
else
ServeAClient((LPVOID) sd_accept);
numServed++;
}
if (isMultiTasking) {
#ifdef _WIN32
err = WaitForMultipleObjects(MAX_SERVED, handles,
TRUE, INFINITE);
printf("Last thread to finish was thread #%d\n", err);
#endif
}
return 0;
}
客户端程序代码:#include <stdio.h>
#include "scompat.h"
/* try to tell linker where WinSock library is */
#if defined(_MSC_VER)
# pragma comment(lib,"wsock32.lib")
#elif defined(__BORLANDC__)
# pragma comment(lib,"mswsock.lib")
#endif
#define WSA_ERROR(x) { printf("Error %d: %s\n", \
WSAGetLastError(), x); return 1; }
void InitSockets(void);
main(int argc, char **argv)
{
SOCKET sd_client;
int err;
u_short iPort;
struct sockaddr_in addr_srv;
struct hostent *ptrHost;
char response[4096];
char *pszHost;
char *msg = "HELLO SERVER";
if (argc == 3) {
pszHost = argv[1];
iPort = atoi(argv[2]);
}
else {
printf("Usage:\t client1 [hostname] [portno]");
return 1;
}
InitSockets();
sd_client = socket(PF_INET, SOCK_STREAM, 0);
if (sd_client == INVALID_SOCKET)
WSA_ERROR("no more socket resources")
if (atoi(pszHost)) {
u_long ip_addr = inet_addr(pszHost);
ptrHost = gethostbyaddr((char *)&ip_addr,
sizeof(u_long), AF_INET);
}
else
ptrHost = gethostbyname(pszHost);
if (!ptrHost)
WSA_ERROR("cannot resolve hostname")
addr_srv.sin_family = PF_INET;
memcpy((char *) &(addr_srv.sin_addr),
ptrHost->h_addr, ptrHost->h_length);
addr_srv.sin_port = htons(iPort);
err = connect(sd_client, (struct sockaddr *) &addr_srv,
sizeof(addr_srv));
if (err == INVALID_SOCKET)
WSA_ERROR("cannot connect to server")
printf("You are connected to the server\n");
send (sd_client, msg, strlen(msg)+1, 0);
memset(response, 0, sizeof(response));
recv(sd_client, response, sizeof(response), 0);
printf("server says %s\n", response);
closesocket(sd_client);
WSACleanup();
return 0;
}
Ⅲ 求C语言高手,实现一个简单的TCPIP程序以实现两台计算机之间的聊天通信,
你上面给出的代码其实就是MSDN里面的演示代码,不过不完整,只演示了两个函数的使用,我给你看看我写的TCP通讯程序,可以在同一个局域网内的两台不同计算机之间聊天:
这其实就是某本将网络通讯的教程里面的例子,不过是我自己重写了一遍,下面给你代码:
========================
下面是公共代码:
========================
#ifndef__CINITSOCK__H__
#define__CINITSOCK__H__
#include<winsock2.h>
#include<iphlpapi.h>
#pragmacomment(lib,"ws2_32.lib")
#pragmacomment(lib,"iphlpapi.lib")
classCInitSock
{
public:
CInitSock(intnMinorVer=2,intnMajorVer=2)
{
WSADATAwsData;
WORDwVer=MAKEWORD(nMinorVer,nMajorVer);
if(0!=WSAStartup(wVer,&wsData))exit(0);
}
~CInitSock()
{
WSACleanup();
}
};
#endif
========================
下面是客户端的代码:
=======================
#include"CInitSock.h"
#include<iostream>
usingnamespacestd;
CInitSockg_Sock;
voidmain()
{
SOCKETsockClient=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(INVALID_SOCKET==sockClient)
return;
sockaddr_insockAddr;
sockAddr.sin_family=AF_INET;
sockAddr.sin_port=htons(4567);
sockAddr.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
if(-1==connect(sockClient,(sockaddr*)&sockAddr,sizeof(sockAddr)))
{
cout<<"connectfailed"<<endl;
return;
}
while(true)
{
charszBuf[MAX_PATH];
ZeroMemory(szBuf,sizeof(szBuf));
cout<<"YouSay:";
cin>>szBuf;
if(SOCKET_ERROR ==send(sockClient,szBuf,MAX_PATH,0))
{
cout<<"sendfailed"<<endl;
return;
}
intnRecvLen=recv(sockClient,szBuf,MAX_PATH,0);
if(nRecvLen>0)
{
//szBuf[nRecvLen]='