没有合适的资源?快使用搜索试试~ 我知道了~
操作系统进程间的通信用C+实现。。。。。。。C++源代码。。。。实现操作系统进程间的通信课设源代码
资源推荐
资源详情
资源评论
进程通信-操作系统设计-C++源代码
发送进程一行一行地向接收进程发送消息,接收进程把接收到的消息打印出来。当接收进程接收到"end"时,两个进程都结束。此程序中利用套接字进行通信,既可以是同一台计算机内的通信,也可以是不同计算机间的通信。
发送进程的程序:
#include "stdafx.h"
#include <winsock2.h>
#include <stdio.h>
#pragma comment(lib,"ws2_32.lib") //把ws2_32.lib加到Link页的连接库
#define IP "127.0.0.1" //通信的IP地址
#define PORT 15001 //通信的端口
char buf[2048]; //buf数组是客户端发送的消息
void message()
{
printf("Input the message:");
int i=0;
memset(buf,0,sizeof(buf));
while((buf=getchar())!='\n') i++;
}
void send()
{
WSADATA WSAData;
if(WSAStartup(MAKEWORD(2,0),&WSAData)==SOCKET_ERROR){
printf("Socket initialize fail!\n");
return;
}
SOCKET sock;
发送进程一行一行地向接收进程发送消息,接收进程把接收到的消息打印出来。当接收进程接收到"end"时,两个进程都结束。此程序中利用套接字进行通信,既可以是同一台计算机内的通信,也可以是不同计算机间的通信。
发送进程的程序:
#include "stdafx.h"
#include <winsock2.h>
#include <stdio.h>
#pragma comment(lib,"ws2_32.lib") //把ws2_32.lib加到Link页的连接库
#define IP "127.0.0.1" //通信的IP地址
#define PORT 15001 //通信的端口
char buf[2048]; //buf数组是客户端发送的消息
void message()
{
printf("Input the message:");
int i=0;
memset(buf,0,sizeof(buf));
while((buf=getchar())!='\n') i++;
}
void send()
{
WSADATA WSAData;
if(WSAStartup(MAKEWORD(2,0),&WSAData)==SOCKET_ERROR){
printf("Socket initialize fail!\n");
return;
}
SOCKET sock;
if((sock=socket(AF_INET,SOCK_STREAM,0))==SOCKET_ERROR){
printf("Socket create fail!\n");
WSACleanup();
return;
}
struct sockaddr_in ClientAddr;
ClientAddr.sin_family=AF_INET;
ClientAddr.sin_port=htons(PORT);
ClientAddr.sin_addr.s_addr=inet_addr(IP);
if(connect(sock,(LPSOCKADDR)&ClientAddr,sizeof(ClientAddr))==SOCKET_ERROR){ //进行服务器连接
printf("Connect fail!\n");
closesocket(sock);
WSACleanup();
return;
}
send(sock,buf,2048,0); //向服务器发送数据
closesocket(sock);
WSACleanup();
}
void main()
{
for(;;){
message();
send();
if(buf[0]=='e' && buf[1]=='n' && buf[2]=='d'){
printf("Program end.\n");
return;
printf("Socket create fail!\n");
WSACleanup();
return;
}
struct sockaddr_in ClientAddr;
ClientAddr.sin_family=AF_INET;
ClientAddr.sin_port=htons(PORT);
ClientAddr.sin_addr.s_addr=inet_addr(IP);
if(connect(sock,(LPSOCKADDR)&ClientAddr,sizeof(ClientAddr))==SOCKET_ERROR){ //进行服务器连接
printf("Connect fail!\n");
closesocket(sock);
WSACleanup();
return;
}
send(sock,buf,2048,0); //向服务器发送数据
closesocket(sock);
WSACleanup();
}
void main()
{
for(;;){
message();
send();
if(buf[0]=='e' && buf[1]=='n' && buf[2]=='d'){
printf("Program end.\n");
return;
剩余5页未读,继续阅读
资源评论
guoyun111
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功