在linux下如何用c语言来写一个socket编程的聊天小程序
展开全部
源码如下:
//chat_one.c
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <malloc.h>
#include <sys/select.h>
int main( int argc, char **argv)
{
struct sockaddr_in chatone, chattwo;
char pmsg[1000];
char *buf;
int chatone_fd;
int ret,i;
int len, msg_len;
fd_set fdset;
if ( argc < 2 ){
printf("please input ip address\n");
return -1;
}
printf("server address is %s\n", argv[1]);
chattwo.sin_family = AF_INET;
chattwo.sin_port = htons(60002);
inet_pton(AF_INET, argv[1], &chattwo.sin_addr.s_addr);
chatone.sin_family = AF_INET;
chatone.sin_port = htons(60000);
chatone.sin_addr.s_addr = INADDR_ANY;
chatone_fd = socket(PF_INET, SOCK_DGRAM, 0);
if ( -1 == chatone_fd ){
printf("create socket failed %s\n", strerror(errno));
return -1;
}
ret = bind(chatone_fd, (struct sockaddr *)&chatone, sizeof(chatone));
if ( -1 == ret){
printf("bind failed %s \n", strerror(errno));
return -1;
}
for(i=0; i<1000;i++){
FD_ZERO( &fdset );
FD_SET ( 0, &fdset);
FD_SET( chatone_fd, &fdset);
if ( -1 == select ( chatone_fd+1, &fdset, NULL, NULL, NULL) ){
continue;
}
if ( FD_ISSET( chatone_fd, &fdset)){
recvfrom( chatone_fd, pmsg, 999, 0, NULL, 0);
printf("receive %s\n", pmsg);
}
else{
memset( pmsg, 0, 1000);
fgets(pmsg, 999, stdin);
len = sizeof(chattwo);
sendto( chatone_fd, pmsg, 1000, 0,\
(struct sockaddr*) &chattwo, len);
printf("send %s\n", pmsg);
}
}
printf("sent %d packets\n", i);
close(chatone_fd);
return 0;
}
//chat_two.c
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <malloc.h>
#include <sys/select.h>
int main( int argc, char **argv)
{
struct sockaddr_in chatone, chattwo;
char pmsg[1000];
char *buf;
int chattwo_fd;
int ret,i;
int len, msg_len;
fd_set fdset;
if ( argc < 2 ){
printf("please input ip address\n");
return -1;
}
printf("server address is %s\n", argv[1]);
chattwo.sin_family = AF_INET;
chattwo.sin_port = htons(60002);
chattwo.sin_addr.s_addr = INADDR_ANY;
chatone.sin_family = AF_INET;
chatone.sin_port = htons(60000);
inet_pton(AF_INET, argv[1], &chatone.sin_addr.s_addr);
chattwo_fd = socket(PF_INET, SOCK_DGRAM, 0);
if ( -1 == chattwo_fd ){
printf("create socket failed %s\n", strerror(errno));
return -1;
}
ret = bind(chattwo_fd, (struct sockaddr *)&chattwo, sizeof(chattwo));
if ( -1 == ret){
printf("bind failed %s \n", strerror(errno));
return -1;
}
for(i=0; i<1000;i++){
FD_ZERO( &fdset );
FD_SET ( 0, &fdset);
FD_SET( chattwo_fd, &fdset);
if ( -1 == select ( chattwo_fd+1, &fdset, NULL, NULL, NULL) ){
continue;
}
if ( FD_ISSET( chattwo_fd, &fdset)){
recvfrom( chattwo_fd, pmsg, 999, 0, NULL, 0);
printf("receive: %s\n", pmsg);
}
else{
memset( pmsg, 0, 1000);
fgets(pmsg, 999, stdin);
len = sizeof(chatone);
sendto( chattwo_fd, pmsg, 1000, 0,\
(struct sockaddr*) &chatone, len);
printf("send %s\n", pmsg);
}
}
printf("sent %d packets\n", i);
close(chattwo_fd);
return 0;
}
编译好这个两个程序就可以进行简单的通信了。
//chat_one.c
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <malloc.h>
#include <sys/select.h>
int main( int argc, char **argv)
{
struct sockaddr_in chatone, chattwo;
char pmsg[1000];
char *buf;
int chatone_fd;
int ret,i;
int len, msg_len;
fd_set fdset;
if ( argc < 2 ){
printf("please input ip address\n");
return -1;
}
printf("server address is %s\n", argv[1]);
chattwo.sin_family = AF_INET;
chattwo.sin_port = htons(60002);
inet_pton(AF_INET, argv[1], &chattwo.sin_addr.s_addr);
chatone.sin_family = AF_INET;
chatone.sin_port = htons(60000);
chatone.sin_addr.s_addr = INADDR_ANY;
chatone_fd = socket(PF_INET, SOCK_DGRAM, 0);
if ( -1 == chatone_fd ){
printf("create socket failed %s\n", strerror(errno));
return -1;
}
ret = bind(chatone_fd, (struct sockaddr *)&chatone, sizeof(chatone));
if ( -1 == ret){
printf("bind failed %s \n", strerror(errno));
return -1;
}
for(i=0; i<1000;i++){
FD_ZERO( &fdset );
FD_SET ( 0, &fdset);
FD_SET( chatone_fd, &fdset);
if ( -1 == select ( chatone_fd+1, &fdset, NULL, NULL, NULL) ){
continue;
}
if ( FD_ISSET( chatone_fd, &fdset)){
recvfrom( chatone_fd, pmsg, 999, 0, NULL, 0);
printf("receive %s\n", pmsg);
}
else{
memset( pmsg, 0, 1000);
fgets(pmsg, 999, stdin);
len = sizeof(chattwo);
sendto( chatone_fd, pmsg, 1000, 0,\
(struct sockaddr*) &chattwo, len);
printf("send %s\n", pmsg);
}
}
printf("sent %d packets\n", i);
close(chatone_fd);
return 0;
}
//chat_two.c
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <malloc.h>
#include <sys/select.h>
int main( int argc, char **argv)
{
struct sockaddr_in chatone, chattwo;
char pmsg[1000];
char *buf;
int chattwo_fd;
int ret,i;
int len, msg_len;
fd_set fdset;
if ( argc < 2 ){
printf("please input ip address\n");
return -1;
}
printf("server address is %s\n", argv[1]);
chattwo.sin_family = AF_INET;
chattwo.sin_port = htons(60002);
chattwo.sin_addr.s_addr = INADDR_ANY;
chatone.sin_family = AF_INET;
chatone.sin_port = htons(60000);
inet_pton(AF_INET, argv[1], &chatone.sin_addr.s_addr);
chattwo_fd = socket(PF_INET, SOCK_DGRAM, 0);
if ( -1 == chattwo_fd ){
printf("create socket failed %s\n", strerror(errno));
return -1;
}
ret = bind(chattwo_fd, (struct sockaddr *)&chattwo, sizeof(chattwo));
if ( -1 == ret){
printf("bind failed %s \n", strerror(errno));
return -1;
}
for(i=0; i<1000;i++){
FD_ZERO( &fdset );
FD_SET ( 0, &fdset);
FD_SET( chattwo_fd, &fdset);
if ( -1 == select ( chattwo_fd+1, &fdset, NULL, NULL, NULL) ){
continue;
}
if ( FD_ISSET( chattwo_fd, &fdset)){
recvfrom( chattwo_fd, pmsg, 999, 0, NULL, 0);
printf("receive: %s\n", pmsg);
}
else{
memset( pmsg, 0, 1000);
fgets(pmsg, 999, stdin);
len = sizeof(chatone);
sendto( chattwo_fd, pmsg, 1000, 0,\
(struct sockaddr*) &chatone, len);
printf("send %s\n", pmsg);
}
}
printf("sent %d packets\n", i);
close(chattwo_fd);
return 0;
}
编译好这个两个程序就可以进行简单的通信了。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
浙江启扬智能科技有限公司
2023-06-12 广告
2023-06-12 广告
ARM,Android,Linux之间并无直接联系。ARM是嵌入式CPU,Andrioid和Linux是操作系统。 ARM不是为Linux而设计,Linux也不是为ARM而设计,二者是独立存在的。Linux和Android可以移植到以ARM...
点击进入详情页
本回答由浙江启扬智能科技有限公司提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询