C语言 linux环境基于socket的简易即时通信程序

转载请注明出处:http://www.cnblogs.com/kevince/p/3891033.html      ——By Kevince

最近在看linux网络编程相关,现学现卖,就写了一个简易的C/S即时通信程序,代码如下:

head.h

 /*头文件,client和server编译时都需要使用*/
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <pthread.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h> #define MSGLEN 1000
#define IPLEN 15 typedef int SOCKET;

server.c:

 /*server*/

 #include "head.h"

 char msg_recv[MSGLEN], msg_send[MSGLEN];
SOCKET server_sockfd, client_sockfd; void *thread_function(void *argv) /*线程函数*/
{
while(){
gets(msg_send);
write(client_sockfd, msg_send, MSGLEN);
}
pthread_exit(NULL);
} int main(int arg, char *argv[])
{
int server_len, client_len;
struct sockaddr_in server_address;
struct sockaddr_in client_address;
int port;
int res; pthread_t a_thread;
void *thread_result; if (arg != ){
printf("server --portnum\n");
exit(EXIT_FAILURE);
} sscanf(argv[], "%d", &port); /*读入端口*/ server_sockfd = socket(AF_INET, SOCK_STREAM, );
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = inet_addr("127.0.0.1");
server_address.sin_port = htons(port); server_len = sizeof(server_address);
bind(server_sockfd, (struct sockaddr *)&server_address, server_len); /*绑定端口并监听*/
listen(server_sockfd, );
printf("listen...\n"); client_len = sizeof(client_address);
client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_address, &client_len);
printf("connection success!\n"); res = pthread_create(&a_thread, NULL, thread_function, NULL); /*启动线程函数*/
if (res != ){
perror("Thread creation failed");
exit(EXIT_FAILURE);
} while(read(client_sockfd, msg_recv, MSGLEN)){
printf("msg from client: %s\n", msg_recv);
}
close(client_sockfd);
exit(EXIT_SUCCESS);
}

client.c:

 /*client*/

 #include "head.h"

 char msg_recv[MSGLEN],msg_send[MSGLEN];
SOCKET sockfd; void *thread_function(void *argv) /*线程函数*/
{
while(){
gets(msg_send);
write(sockfd, msg_send, MSGLEN);
}
pthread_exit(NULL);
} int main(int arg, char *argv[])
{
struct sockaddr_in address;
int len;
int res;
int port;
char ip[IPLEN]; void *thread_result;
pthread_t a_thread; sockfd = socket(AF_INET, SOCK_STREAM, ); if (arg != ){
printf("client --ipaddress --portnum\n");
exit(EXIT_FAILURE);
} sscanf(argv[], "%s", ip);
sscanf(argv[], "%d", &port); /*读取ip与端口*/ address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr(ip);
address.sin_port = htons(port); len = sizeof(address);
res = connect(sockfd, (struct sockaddr *)&address, len);
if (res == -){
perror("connect failed! ");
exit(EXIT_FAILURE);
}
printf("connection success!\n"); res = pthread_create(&a_thread, NULL, thread_function, NULL); /*启动线程函数*/
if (res != ){
perror("Thread creation failed");
exit(EXIT_FAILURE);
} while(read(sockfd, msg_recv, MSGLEN)){
printf("msg from server: %s\n", msg_recv);
}
res = pthread_join(a_thread, &thread_result);
if (res != ){
perror("joined failed");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}

由于使用了线程,所以要链接正确的线程库,所以编译命令如下:

gcc -D_REENTRANT -I/usr/include/nptl server.c -o server -L/usr/lib/nptl -lpthread

gcc -D_REENTRANT -I/usr/include/nptl client.c -o client -L/usr/lib/nptl -lpthread

如果你的系统默认使用的就是NPTL线程库,那么编译时就无需加上-I和-L选项

运行时输入的命令规则是:

./server --portnum  #即server后面要加上需要绑定的端口号。

./client --ip --portnum  #即client后面要加上服务器的IP地址以及端口号。

不积跬步无以至千里,虽然这两个程序很简单,但作为我的第一个linux环境下基于socket的通信程序,也很有纪念意义。

上一篇:[msf]那些年儿跑过的字典


下一篇:Python解释器