linux下编写聊天软件非常简单,创建服务后,再创建两个线程进行收发就可以了。下面把我的简单代码贴出供大家参考。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <errno.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
struct sockaddr_in client_addr;
int sock;
socklen_t addr_len;
sem_t send_sem;
char send_message_buffer[128];
void *recv_message(void *arg)
{
char buffer[128];
int len;
while(1)
{
len = recvfrom(sock, buffer, sizeof(buffer) - 1, 0,(struct sockaddr *) &client_addr, &addr_len);
if (len < 0)
{
perror("接收数据失败");
}
buffer[len] = '/0';
printf("-----接收数据-----/n");
printf("收到来自%s:%d的消息:%s/n",inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), buffer);
}
}
void *send_message(void *arg)
{
int len;
while(1)
{
sem_wait(&send_sem);
len = sendto(sock, send_message_buffer, strlen(send_message_buffer), 0,(struct sockaddr *) &client_addr, addr_len);
if (len < 0)
{
printf("/n发送数据失败/n");
}
printf("-----发送数据-----/n");
printf("发送消息:%s 到%s:%d/n",send_message_buffer,inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
}
}
int main(int argc, char **argv)
{
struct sockaddr_in server_addr;
int stdin_fd;
int len;
int res;
char ip_address[128];
char buffer[128];
int port_number;
pthread_t recv_thread,send_thread;
pthread_attr_t thread_attr;
/* 创建 socket , 关键在于这个 SOCK_DGRAM */
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
perror("s创建套接字失败");
exit(errno);
}
else
{
printf("创建套接字成功/n");
}
memset(&server_addr, 0, sizeof(struct sockaddr_in));
/* 设置地址和端口信息 */
server_addr.sin_family = AF_INET;
if (argv[2])
{
server_addr.sin_port = htons(atoi(argv[2]));
}
else
{
server_addr.sin_port = htons(6666);
}
if (argv[1])
{
server_addr.sin_addr.s_addr = inet_addr(argv[1]);
}
else
{
server_addr.sin_addr.s_addr = INADDR_ANY;
}
/* 绑定地址和端口信息 */
if ((bind(sock, (struct sockaddr *) &server_addr, sizeof(server_addr))) == -1)
{
perror("绑定端口失败/n");
exit(errno);
}
else
{
printf("绑定端口成功/n");
}
printf("输入想连接电脑的IP地址:");
gets(ip_address);
printf("输入想连接电脑的端口号:");
gets(buffer);
port_number=atoi(buffer);
client_addr.sin_addr.s_addr = inet_addr(ip_address);
client_addr.sin_port = htons(port_number);
stdin_fd=open("/dev/stdin",O_RDWR);
if(stdin_fd==-1)
{
printf("打开标准输入错误/n");
close(sock);
return -1;
}
/* 循环接收数据 */
addr_len = sizeof(client_addr);
res=sem_init(&send_sem,0,0); /*初始化信号量*/
if(res!=0)
{
perror("初始化信号错误/n");
exit(EXIT_FAILURE);
}
res=pthread_attr_init(&thread_attr); /*设置属性为脱离状态*/
if(res!=0)
{
perror("初始化线程属性错误/n");
exit(EXIT_FAILURE);
}
res=pthread_attr_setdetachstate(&thread_attr,PTHREAD_CREATE_DETACHED);/*设置属性为脱离状态::即不等待子线程的返回值*/
if(res!=0)
{
perror("创建线程属性失败/n");
exit(EXIT_FAILURE);
}
res=pthread_create(&recv_thread,&thread_attr,recv_message,NULL); /*调用发送函数*/
if(res!=0)
{
perror("创建线程失败/n");
exit(EXIT_FAILURE);
}
res=pthread_create(&send_thread,&thread_attr,send_message,NULL); /*调用发送函数*/
if(res!=0)
{
perror("创建线程失败/n");
exit(EXIT_FAILURE);
}
while (1)
{
len=read(stdin_fd, send_message_buffer,128);
send_message_buffer[len-1] = '/0';
sem_post(&send_sem);
//printf("有数据发送/n");
}
return 0;
}
我不喜欢写一大堆文字,喜欢用代码来解释。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <errno.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
struct sockaddr_in client_addr;
int sock;
socklen_t addr_len;
sem_t send_sem;
char send_message_buffer[128];
void *recv_message(void *arg)
{
char buffer[128];
int len;
while(1)
{
len = recvfrom(sock, buffer, sizeof(buffer) - 1, 0,(struct sockaddr *) &client_addr, &addr_len);
if (len < 0)
{
perror("接收数据失败");
}
buffer[len] = '/0';
printf("-----接收数据-----/n");
printf("收到来自%s:%d的消息:%s/n",inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), buffer);
}
}
void *send_message(void *arg)
{
int len;
while(1)
{
sem_wait(&send_sem);
len = sendto(sock, send_message_buffer, strlen(send_message_buffer), 0,(struct sockaddr *) &client_addr, addr_len);
if (len < 0)
{
printf("/n发送数据失败/n");
}
printf("-----发送数据-----/n");
printf("发送消息:%s 到%s:%d/n",send_message_buffer,inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
}
}
int main(int argc, char **argv)
{
struct sockaddr_in server_addr;
int stdin_fd;
int len;
int res;
char ip_address[128];
char buffer[128];
int port_number;
pthread_t recv_thread,send_thread;
pthread_attr_t thread_attr;
/* 创建 socket , 关键在于这个 SOCK_DGRAM */
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
perror("s创建套接字失败");
exit(errno);
}
else
{
printf("创建套接字成功/n");
}
memset(&server_addr, 0, sizeof(struct sockaddr_in));
/* 设置地址和端口信息 */
server_addr.sin_family = AF_INET;
if (argv[2])
{
server_addr.sin_port = htons(atoi(argv[2]));
}
else
{
server_addr.sin_port = htons(6666);
}
if (argv[1])
{
server_addr.sin_addr.s_addr = inet_addr(argv[1]);
}
else
{
server_addr.sin_addr.s_addr = INADDR_ANY;
}
/* 绑定地址和端口信息 */
if ((bind(sock, (struct sockaddr *) &server_addr, sizeof(server_addr))) == -1)
{
perror("绑定端口失败/n");
exit(errno);
}
else
{
printf("绑定端口成功/n");
}
printf("输入想连接电脑的IP地址:");
gets(ip_address);
printf("输入想连接电脑的端口号:");
gets(buffer);
port_number=atoi(buffer);
client_addr.sin_addr.s_addr = inet_addr(ip_address);
client_addr.sin_port = htons(port_number);
stdin_fd=open("/dev/stdin",O_RDWR);
if(stdin_fd==-1)
{
printf("打开标准输入错误/n");
close(sock);
return -1;
}
/* 循环接收数据 */
addr_len = sizeof(client_addr);
res=sem_init(&send_sem,0,0); /*初始化信号量*/
if(res!=0)
{
perror("初始化信号错误/n");
exit(EXIT_FAILURE);
}
res=pthread_attr_init(&thread_attr); /*设置属性为脱离状态*/
if(res!=0)
{
perror("初始化线程属性错误/n");
exit(EXIT_FAILURE);
}
res=pthread_attr_setdetachstate(&thread_attr,PTHREAD_CREATE_DETACHED);/*设置属性为脱离状态::即不等待子线程的返回值*/
if(res!=0)
{
perror("创建线程属性失败/n");
exit(EXIT_FAILURE);
}
res=pthread_create(&recv_thread,&thread_attr,recv_message,NULL); /*调用发送函数*/
if(res!=0)
{
perror("创建线程失败/n");
exit(EXIT_FAILURE);
}
res=pthread_create(&send_thread,&thread_attr,send_message,NULL); /*调用发送函数*/
if(res!=0)
{
perror("创建线程失败/n");
exit(EXIT_FAILURE);
}
while (1)
{
len=read(stdin_fd, send_message_buffer,128);
send_message_buffer[len-1] = '/0';
sem_post(&send_sem);
//printf("有数据发送/n");
}
return 0;
}
我不喜欢写一大堆文字,喜欢用代码来解释。