#include "stdafx.h"
#include <Windows.h>
#include <strsafe.h>
#include <stdio.h>
HANDLE hSlot;
LPTSTR Slot = TEXT("\\\\.\\mailslot\\test_mailslot");
BOOL ReadSlot()
{
DWORD cbMessage, cMessage, cbRead;
BOOL fResult;
LPTSTR lpszBuffer;
TCHAR achID[80];
DWORD cAllMessages;
HANDLE hEvent;
OVERLAPPED ov;
cbMessage = cMessage = cbRead = 0;
hEvent = CreateEvent(NULL, FALSE, FALSE, TEXT("ExampleSlot"));
if (NULL == hEvent)
return FALSE;
ov.Offset = 0;
ov.OffsetHigh = 0;
ov.hEvent = hEvent;
fResult = GetMailslotInfo(hSlot, (LPDWORD)NULL, &cbMessage, &cMessage, (LPDWORD)NULL);
if (!fResult)
{
printf("Get MailSlot failed %d.\n", GetLastError());
return FALSE;
}
if (cbMessage == MAILSLOT_NO_MESSAGE)
{
printf("Waiting for a message\n");
return TRUE;
}
cAllMessages = cMessage;
while (cMessage != 0)
{
StringCchPrintf((LPTSTR)achID, 80, TEXT("\nMessage #%d of %d\n"), cAllMessages - cMessage + 1, cAllMessages);
lpszBuffer = (LPTSTR)GlobalAlloc(GPTR, lstrlen((LPTSTR)achID)*sizeof(TCHAR) + cbMessage);
if (NULL == lpszBuffer) return FALSE;
lpszBuffer[0] = '\0';
fResult = ReadFile(hSlot, lpszBuffer, cbMessage, &cbRead, &ov);
if (!fResult)
{
printf("Read Fail %d\n", GetLastError());
GlobalFree((HGLOBAL)lpszBuffer);
return FALSE;
}
StringCbCat(lpszBuffer, lstrlen((LPTSTR)achID)*sizeof(TCHAR) + cbMessage, (LPTSTR)achID);
_tprintf(TEXT("Contents of the mail:%s\n"), lpszBuffer);
GlobalFree((HGLOBAL)lpszBuffer);
fResult = GetMailslotInfo(hSlot, (LPDWORD)NULL, &cbMessage, &cMessage, (LPDWORD)NULL);
if (!fResult)
{
printf("Get Mail Slot Info %d\n", GetLastError());
}
}
CloseHandle(hEvent);
return TRUE;
}
int main()
{
hSlot = CreateMailslot(Slot, 0, MAILSLOT_WAIT_FOREVER, (LPSECURITY_ATTRIBUTES) NULL);
if (hSlot == INVALID_HANDLE_VALUE)
{
printf("Create Mail Slot Failed:%d \n", GetLastError());
return 0;
}
else printf("Mail slot create ok!!\n");
while (TRUE)
{
ReadSlot();
Sleep(3000);
}
}
=============================================================================================
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
LPTSTR SlotName = TEXT("\\\\.\\mailslot\\test_mailslot");
int main()
{
HANDLE hfile;
hfile = CreateFile(SlotName, GENERIC_WRITE, FILE_SHARE_READ, (LPSECURITY_ATTRIBUTES)NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, (HANDLE)NULL);
if (hfile == INVALID_HANDLE_VALUE)
{
printf("CreateFile failed with %d\n", GetLastError());
return FALSE;
}
BOOL fResult;
DWORD cbWritten;
fResult = WriteFile(hfile, SlotName, (DWORD)(lstrlen(SlotName) + 1)*sizeof(TCHAR), &cbWritten, (LPOVERLAPPED)NULL);
if (!fResult)
{
printf("Write file failed:%d \n", GetLastError());
return FALSE;
}
printf("Slot written to successfully \n");
CloseHandle(hfile);
for (int h = 0; h < 10000; h++) Sleep(2000);
return 0;
}
執行結果:
2016年11月14日 星期一
2016年11月13日 星期日
CGI study
不錯的文章:
閒聊CGI(Common Gateway Interface)程式
http://blog.yam.com/csylvia/article/19952784
www service Ifo
http://blog.xuite.net/coke750101/networkprogramming/56098687
httpClient抓取网页–linux C/C++
http://www.xuebuyuan.com/2121856.html
Linux下C抓取网页
http://blog.csdn.net/hejinwei_1987/article/details/7540500
閒聊CGI(Common Gateway Interface)程式
http://blog.yam.com/csylvia/article/19952784
www service Ifo
http://blog.xuite.net/coke750101/networkprogramming/56098687
httpClient抓取网页–linux C/C++
http://www.xuebuyuan.com/2121856.html
Linux下C抓取网页
http://blog.csdn.net/hejinwei_1987/article/details/7540500
2016年11月7日 星期一
[C#]Interprocess Communications (File Mapping)
Send:
using System.IO.MemoryMappedFiles;
namespace ConsoleApplication_IPC_FileMappingSend
{
class Program
{
static void Main(string[] args)
{
MemoryMappedFile mmf = MemoryMappedFile.CreateNew("test", 1000);
MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor();
accessor.Write(500, 42);
Console.WriteLine("Memory-mapped file created!");
Console.ReadLine();
accessor.Dispose();
mmf.Dispose();
}
}
}
Recvive:
using System.IO.MemoryMappedFiles;
namespace ConsoleApplication_IPC_FileMappingRec
{
class Program
{
static void Main(string[] args)
{
MemoryMappedFile mmf = MemoryMappedFile.OpenExisting("test");
MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor();
int value = accessor.ReadInt32(500);
Console.WriteLine("the answer is {0}", value);
Boolean zeon1 = accessor.CanRead;
mmf.Dispose();
Console.ReadLine();
}
}
}
using System.IO.MemoryMappedFiles;
namespace ConsoleApplication_IPC_FileMappingSend
{
class Program
{
static void Main(string[] args)
{
MemoryMappedFile mmf = MemoryMappedFile.CreateNew("test", 1000);
MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor();
accessor.Write(500, 42);
Console.WriteLine("Memory-mapped file created!");
Console.ReadLine();
accessor.Dispose();
mmf.Dispose();
}
}
}
Recvive:
using System.IO.MemoryMappedFiles;
namespace ConsoleApplication_IPC_FileMappingRec
{
class Program
{
static void Main(string[] args)
{
MemoryMappedFile mmf = MemoryMappedFile.OpenExisting("test");
MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor();
int value = accessor.ReadInt32(500);
Console.WriteLine("the answer is {0}", value);
Boolean zeon1 = accessor.CanRead;
mmf.Dispose();
Console.ReadLine();
}
}
}
[C#]Interprocess Communications (Data Copy)
Send:
using
System.Runtime.InteropServices;
namespace
WindowsFormsApplication_IPC_DataCopy_Send
{
public partial class Form1 : Form
{
[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr
hWnd, uint Msg, int wParam, IntPtr lParam);
[DllImport("user32.dll")]
private static extern int
SendMessage(IntPtr hwnd, uint wMsg, int wPar);
[DllImport("user32.dll",
SetLastError = true, CharSet = CharSet.Auto)]
static extern uint RegisterWindowMessage(string
lpString);
uint MSG_SHOW =
RegisterWindowMessage("Show Message");
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string
lpClassName, string lpWindowName);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object
sender, EventArgs e)
{
IntPtr nPrt = FindWindow(null,
"TT");
if(nPrt != IntPtr.Zero &&
textBox1.Text.Trim()!="")
{
try
{
string tempword =
textBox1.Text.Trim();
int iNum =
int.Parse(tempword);
SendMessage(nPrt, MSG_SHOW,
iNum , IntPtr.Zero);
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
}
Receive:
using
System.Runtime.InteropServices;
namespace
WindowsFormsApplication_IPC_DataCopy_Rec
{
public partial class Form1 : Form
{
[DllImport("user32.dll",
SetLastError = true, CharSet = CharSet.Auto)]
static extern uint
RegisterWindowMessage(string lpString);
uint MSG_SHOW =
RegisterWindowMessage("Show Message");
public Form1()
{
InitializeComponent();
}
protected override void WndProc(ref
Message m)
{
if(m.Msg==MSG_SHOW)
{
label1.Text =
(string)m.WParam.ToString();
}
base.WndProc(ref m);
}
}
}
2016年11月6日 星期日
[C#]Interprocess Communications (Clipboard)
使用剪貼簿的方法:
Clipboard.SetText
Clipboard.SetImage
Clipboard.SetData
Clipboard.GetText
Clipboard.GetImage
Clipboard.GatData
好像一次只能有一筆,不能一次Set多筆,然後慢慢接收.......@#$$$%%%%%
參考:
Clipboard.SetText
Clipboard.SetImage
Clipboard.SetData
Clipboard.GetText
Clipboard.GetImage
Clipboard.GatData
好像一次只能有一筆,不能一次Set多筆,然後慢慢接收.......@#$$$%%%%%
參考:
2016年11月3日 星期四
[C#]程式視窗縮到右下小工作列內
在 Toolbox 內選用 NotiftIcon,Properties 內,選 Icon 及當指標到圖示時,需顯示的 Text
namespace WindowsFormsApplication_NotifyEx
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_SizeChanged(object sender, EventArgs e)
{
if(this.WindowState==FormWindowState.Minimized)
{
this.Hide();
this.notifyIcon1.Visible = true;
}
}
//右下角圖示按兩下滑鼠指標
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.notifyIcon1.Visible = false;
}
}
}
C# 讓表單執行時最大化、最小化、預設大小、最上層顯示
https://ithelp.ithome.com.tw/articles/10029790
[C#] WPF Notify Icon
namespace WindowsFormsApplication_NotifyEx
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_SizeChanged(object sender, EventArgs e)
{
if(this.WindowState==FormWindowState.Minimized)
{
this.Hide();
this.notifyIcon1.Visible = true;
}
}
//右下角圖示按兩下滑鼠指標
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.notifyIcon1.Visible = false;
}
}
}
C# 讓表單執行時最大化、最小化、預設大小、最上層顯示
https://ithelp.ithome.com.tw/articles/10029790
http://cowbaycoding.blogspot.com/2017/02/c.html
[Raspberry] TCP/IP 傳輸之 server/client 程式
Socket 在哪邊:
TCP:
UDP:
hostaddress 由4 byte的數字組成,socket中的struct in_addr 將這個整束包裝起來,定義在in.h
struct in_addr {
__be32 s_addr;
}
特殊的host address number:
l INADDR_LOOPBACK:指本機的address,也就是127.0,0,1(localhost)
l INADDR_ANY:指任何連上來的address
l INADDR_BROADCAST:傳送broadcast訊息才可使用
l INADDR_NONE:某些function錯誤時的回傳值
字節順序轉換函數:
htonl():把32位值從主機字節序轉換成網絡字節序
htons():把16位值從主機字節序轉換成網絡字節序
ntohl():把32位值從網絡字節序轉換成主機字節序
ntohs():把16位值從網絡字節序轉換成主機字節序
Bind函數將socket與本機上的一個埠相關聯.你就可以在該埠監聽服務請求
Client端:
#include <cstdlib>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
using namespace std;
/*
*
*/
int main(int argc, char** argv) {
int sockfd=0,n=0;
char recvBuff[1024];
struct sockaddr_in serv_addr;
if(argc!=2)
{
printf("\n Usage:%s <ip of server> \n",argv[0]);
return 1;
}
memset(recvBuff,'0',sizeof(recvBuff));
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
printf("\n Error: Could not create socket \n");
return 1;
}
memset(&serv_addr,'0',sizeof(serv_addr));
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(5000);
if(inet_pton(AF_INET,argv[1],&serv_addr.sin_addr)<=0)
{
printf("\n inet_pton error occured\n");
return 1;
}
if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0)
{
printf("\n Error:Connect Failed \n");
return 1;
}
while((n=read(sockfd,recvBuff,sizeof(recvBuff)-1))>0)
{
recvBuff[n]=0;
if(fputs(recvBuff,stdout)==EOF)
{
printf("\n Error:Fputs error \n");
}
}
if(n<0)
{
printf("\n Read error\n");
}
return 0;
}
SERVER端:
#include <cstdlib>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <time.h>
using namespace std;
/*
*
*/
int main(int argc, char** argv) {
int listenfd=0,connfd=0;
struct sockaddr_in serv_addr;
char sendBuff[1025];
time_t ticks;
listenfd=socket(AF_INET,SOCK_STREAM,0);
memset(&serv_addr,'0',sizeof(serv_addr));
memset(sendBuff,'0',sizeof(sendBuff));
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
serv_addr.sin_port=htons(5000);
bind(listenfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
listen(listenfd,10);
while(1)
{
connfd=accept(listenfd,(struct sockaddr *)NULL,NULL);
ticks=time(NULL);
snprintf(sendBuff,sizeof(sendBuff),"%.24s\r\n",ctime(&ticks));
write(connfd,sendBuff,strlen(sendBuff));
close(connfd);
sleep(1);
}
return 0;
}
#include <cstdlib>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
using namespace std;
/*
*
*/
int main(int argc, char** argv) {
int sockfd=0,n=0;
char recvBuff[1024];
struct sockaddr_in serv_addr;
if(argc!=2)
{
printf("\n Usage:%s <ip of server> \n",argv[0]);
return 1;
}
memset(recvBuff,'0',sizeof(recvBuff));
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
printf("\n Error: Could not create socket \n");
return 1;
}
memset(&serv_addr,'0',sizeof(serv_addr));
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(5000);
if(inet_pton(AF_INET,argv[1],&serv_addr.sin_addr)<=0)
{
printf("\n inet_pton error occured\n");
return 1;
}
if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0)
{
printf("\n Error:Connect Failed \n");
return 1;
}
while((n=read(sockfd,recvBuff,sizeof(recvBuff)-1))>0)
{
recvBuff[n]=0;
if(fputs(recvBuff,stdout)==EOF)
{
printf("\n Error:Fputs error \n");
}
}
if(n<0)
{
printf("\n Read error\n");
}
return 0;
}
SERVER端:
#include <cstdlib>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <time.h>
using namespace std;
/*
*
*/
int main(int argc, char** argv) {
int listenfd=0,connfd=0;
struct sockaddr_in serv_addr;
char sendBuff[1025];
time_t ticks;
listenfd=socket(AF_INET,SOCK_STREAM,0);
memset(&serv_addr,'0',sizeof(serv_addr));
memset(sendBuff,'0',sizeof(sendBuff));
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
serv_addr.sin_port=htons(5000);
bind(listenfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
listen(listenfd,10);
while(1)
{
connfd=accept(listenfd,(struct sockaddr *)NULL,NULL);
ticks=time(NULL);
snprintf(sendBuff,sizeof(sendBuff),"%.24s\r\n",ctime(&ticks));
write(connfd,sendBuff,strlen(sendBuff));
close(connfd);
sleep(1);
}
return 0;
}
參考:
http://www.thegeekstuff.com/2011/12/c-socket-programming/
訂閱:
文章 (Atom)



