perror("msgget");
exit(1);
}
printf("msgqueue_id=%dn",msgqueue_id);
}
switch(tolower(argv[1][0]))
{
case "s": send_message(atoi(argv[4]), (struct mymsgbuf *)&qbuf,
atol(argv[2]), argv[3]);
break;
case "r": read_message(msgqueue_id, &qbuf, atol(argv[2]));
break;
case "d": remove_queue(atoi(argv[2]));
remove_queue(msgqueue_id);
break;
case "m": change_queue_mode(msgqueue_id, argv[2]);
break;
default: usage();
}
return(0);
}
void send_message(int qid, struct mymsgbuf *qbuf, long type, char *text)
{
printf("msgqueue_id=%dn",qid);
/* Send a message to the queue */
printf("Sending a message ...n");
qbuf->mtype = type;
strcpy(qbuf->mtext, text);
printf(" Type: %ld Text: %sn", qbuf->mtype, qbuf->mtext);
if((msgsnd(qid, (struct msgbuf *)qbuf,
strlen(qbuf->mtext) 1, 0)) ==-1)
{
perror("msgsnd");
exit(1);
}
}
void read_message(int qid, struct mymsgbuf *qbuf, long type)
{
/* Read a message from the queue */
printf("Reading a message ...n");
qbuf->mtype = type;
msgrcv(qid, (struct msgbuf *)qbuf, MAX_SEND_SIZE, type, 0);
printf(" Type: %ld Text: %sn", qbuf->mtype, qbuf->mtext);
}
void remove_queue(int qid)
{
/* Remove the queue */
msgctl(qid, IPC_RMID, 0);
}
void change_queue_mode(int qid, char *mode)
{
struct msqid_ds myqueue_ds;
/* Get current info */
msgctl(qid, IPC_STAT, &myqueue_ds);
/* Convert and load the mode */
sscanf(mode, "%ho", &myqueue_ds.msg_perm.mode);
/* Update the mode */
msgctl(qid, IPC_SET, &myqueue_ds);
}
void usage(void)
{
fprintf(stderr, "msgtool - A utility for tinkering with msg queuesn");
fprintf(stderr, "nUSAGE: msgtool (s)end
fprintf(stderr, " (r)ecv n");
fprintf(stderr, " (d)eleten");
fprintf(stderr, " (m)ode
fprintf(stderr, "note: type must be number!n");
exit(1);
}
2,Tcp/IP socket编程例子
1), ClIEnt方
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
int sockfd ,newsockfd, help, sent;
struct sockaddr_in peer;
struct hostent *serverhost;
char buff[5000];
if(argc<2) {
fprintf(stderr, "Usage: coc n");
exit(1);
}
if ((sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0 ) {
perror("socket");
exit(1);
}
if((serverhost = gethostbyname(argv[1])) == 0) {
perror("gethostbyname");
exit(1);
}
peer.sin_family = AF_INET;
peer.sin_port = htons(10000);
peer.sin_addr = *(struct in_addr*)serverhost->h_addr_list[0];
if (connect(sockfd, &peer, sizeof(peer)) < 0 ) {
perror("connect");
exit(1);
}
for(help=0; helpbuff[help] = "0" help;
write(sockfd, buff, 5000);
close(sockfd);
}
2, Server方
#include
#include
#include
#include
void process(int fd)
{
char buff[10000];
int received;
int help,read_bytes;
received = 5000;
memset ( buff, ".", received );
read_bytes = read(fd, buff, received);
if (read_bytes < 0) {
perror("read");
exit(1);
}
printf("%d bytes have received on socket %dn", read_bytes, fd);
printf("buff=n%sn", buff);
for(help=0; help
{
printf("Error on position %dn", help);
break;
}
}
int main(void)
{
int sockfd ,newsockfd;
struct sockaddr_in myaddr, peer;
int addrlen1,addrlen2;
if ((sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0 ) {
perror("socket");
exit(1);
}
addrlen1 = sizeof(myaddr);
myaddr.sin_family = AF_INET;
myaddr.sin_port = htons(10000);
myaddr.sin_addr.s_addr = INADDR_ANY;
if (bind(sockfd, &myaddr , addrlen1) < 0 ) {
perror("bind");
exit(1);
}
推荐阅读
- 2 SCO UNIX讲座
- 班门弄斧 用机十天之体验
- SCO UNIX系统故障特征、分析及解决
- 在Unix终端上实现多屏功能
- 口令篇 UNIX系统的安全
- super试飞首信V851之一:初步印象篇
- unixware和unix openserver比较二
- Cisco路由器配置信息在Unix下的备份、恢复与更新
- SCO UNIX基础讲座--第十二讲:使用 TCP/IP
- 明日之后中获得钢铁入侵抗击者纪念币具体操作方法