[问题]即时通信的C语言代码

C、C++和Java语言
回复
luchen
帖子: 52
注册时间: 2007-07-16 18:40
来自: 浙江
送出感谢: 0
接收感谢: 0

[问题]即时通信的C语言代码

#1

帖子 luchen » 2007-10-12 23:18

哪位能提供一下小弟在ubuntu下即时通信的C语言代码
听老师说只要50行左右,以前没弄过,希望各位能提供一个让小弟参考一下
showboy
帖子: 122
注册时间: 2006-11-26 14:41
送出感谢: 0
接收感谢: 0

#2

帖子 showboy » 2007-10-13 1:34

如果不用 套接字,十几行就搞定了
头像
speme
帖子: 723
注册时间: 2005-10-02 8:22
送出感谢: 0
接收感谢: 0
联系:

#3

帖子 speme » 2007-10-13 14:48

用套接字,估计几十行也能搞定。
子曰,三人行,必有我师,吾曰,世间万物,皆为我师.
头像
ttand
帖子: 1743
注册时间: 2005-08-22 14:05
来自: 离开北京
送出感谢: 1 次
接收感谢: 3 次

#4

帖子 ttand » 2007-10-13 20:18

udp的 基本就是这样了
连接端

代码: 全选

#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <unistd.h>

#define MAXLINE 80
#define SERV_PORT 8888

void do_cli(FILE *fp, int sockfd, struct sockaddr *pservaddr, socklen_t servlen){
	int n;
	char sendline[MAXLINE], recvline[MAXLINE + 1];

	/* connect to server */
	if(connect(sockfd, (struct sockaddr *)pservaddr, servlen) == -1){
		perror("connect error");
		exit(1);
	}

	while(fgets(sendline, MAXLINE, fp) != NULL){
		/* read a line and send to server */
		write(sockfd, sendline, strlen(sendline));
		/* receive data from server */
		n = read(sockfd, recvline, MAXLINE);
		if(n == -1){
			perror("read error");
			exit(1);
		}
		recvline[n] = 0; /* terminate string */
		fputs(recvline, stdout);
	}
}

int main(int argc, char **argv){
	int sockfd;
	struct sockaddr_in servaddr;
	/* check args */
	if(argc != 2){
		printf("usage: udpclient <IPaddress>\n");
		exit(1);
	}
	/* init servaddr */
	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(SERV_PORT);
	if(inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0){
		printf("[%s] is not a valid IPaddress\n", argv[1]);
		exit(1);
	}

	sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	do_cli(stdin, sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
	return 0;
}
侦听端

代码: 全选

#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>

#define MAXLINE 80
#define SERV_PORT 8888

void do_echo(int sockfd, struct sockaddr *pcliaddr, socklen_t clilen){
	int n;
	socklen_t len;
	char mesg[MAXLINE];

	while(1){
		len = clilen;
		/* waiting for receive data */
		n = recvfrom(sockfd, mesg, MAXLINE, 0, pcliaddr, &len);
		/* sent data back to client */
		sendto(sockfd, mesg, n, 0, pcliaddr, len);
		fputs(mesg, stdout);
	}
}

int main(void){
	int sockfd;
	struct sockaddr_in servaddr, cliaddr;
	
	sockfd = socket(AF_INET, SOCK_DGRAM, 0); /* create a socket */
	
	/* init servaddr */
	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	servaddr.sin_port = htons(SERV_PORT);
	
	/* bind address and port to socket */
	if(bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1){
		perror("bind error");
		exit(1);
	}
	do_echo(sockfd, (struct sockaddr *)&cliaddr, sizeof(cliaddr));

	return 0;
}
luchen
帖子: 52
注册时间: 2007-07-16 18:40
来自: 浙江
送出感谢: 0
接收感谢: 0

#5

帖子 luchen » 2007-10-13 21:02

谢谢各位
skyily
帖子: 29
注册时间: 2007-10-05 1:36
来自: 江西
送出感谢: 0
接收感谢: 0
联系:

好东西

#6

帖子 skyily » 2007-10-20 2:33

谢谢,版主了,
我喜欢,我追求......
cg19820811
帖子: 40
注册时间: 2007-04-09 15:30
送出感谢: 0
接收感谢: 0
联系:

#7

帖子 cg19820811 » 2007-10-22 22:54

ttand 写了:udp的 基本就是这样了
连接端

代码: 全选

#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <unistd.h>

#define MAXLINE 80
#define SERV_PORT 8888

void do_cli(FILE *fp, int sockfd, struct sockaddr *pservaddr, socklen_t servlen){
	int n;
	char sendline[MAXLINE], recvline[MAXLINE + 1];

	/* connect to server */
	if(connect(sockfd, (struct sockaddr *)pservaddr, servlen) == -1){
		perror("connect error");
		exit(1);
	}

	while(fgets(sendline, MAXLINE, fp) != NULL){
		/* read a line and send to server */
		write(sockfd, sendline, strlen(sendline));
		/* receive data from server */
		n = read(sockfd, recvline, MAXLINE);
		if(n == -1){
			perror("read error");
			exit(1);
		}
		recvline[n] = 0; /* terminate string */
		fputs(recvline, stdout);
	}
}

int main(int argc, char **argv){
	int sockfd;
	struct sockaddr_in servaddr;
	/* check args */
	if(argc != 2){
		printf("usage: udpclient <IPaddress>\n");
		exit(1);
	}
	/* init servaddr */
	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(SERV_PORT);
	if(inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0){
		printf("[%s] is not a valid IPaddress\n", argv[1]);
		exit(1);
	}

	sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	do_cli(stdin, sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
	return 0;
}
侦听端

代码: 全选

#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>

#define MAXLINE 80
#define SERV_PORT 8888

void do_echo(int sockfd, struct sockaddr *pcliaddr, socklen_t clilen){
	int n;
	socklen_t len;
	char mesg[MAXLINE];

	while(1){
		len = clilen;
		/* waiting for receive data */
		n = recvfrom(sockfd, mesg, MAXLINE, 0, pcliaddr, &len);
		/* sent data back to client */
		sendto(sockfd, mesg, n, 0, pcliaddr, len);
		fputs(mesg, stdout);
	}
}

int main(void){
	int sockfd;
	struct sockaddr_in servaddr, cliaddr;
	
	sockfd = socket(AF_INET, SOCK_DGRAM, 0); /* create a socket */
	
	/* init servaddr */
	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	servaddr.sin_port = htons(SERV_PORT);
	
	/* bind address and port to socket */
	if(bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1){
		perror("bind error");
		exit(1);
	}
	do_echo(sockfd, (struct sockaddr *)&cliaddr, sizeof(cliaddr));

	return 0;
}

这listen端有点错。要加上一句

void do_echo(int sockfd, struct sockaddr *pcliaddr, socklen_t clilen){
int n;
socklen_t len;
char mesg[MAXLINE];

while(1){
len = clilen;
/* waiting for receive data */
n = recvfrom(sockfd, mesg, MAXLINE, 0, pcliaddr, &len);
/* sent data back to client */
mesg[n]='\0';
sendto(sockfd, mesg, n, 0, pcliaddr, len);
fputs(mesg, stdout);
}
}
回复

回到 “C/C++/Java”