用C当然行得通
就是个socket编程嘛
我们原来做过一个的原理描述
当然
这个是C\S模式的
其实你可以做成
无服务端的
本系统具有局域网聊天功能。采用了C\S模式(即服务器创建套接字后,转换为监听套接字,一直在监听是否由客户端的请求。服务器接收到相应的请求后,进行相应的处理)。采用了TCP/IP(面向连接)协议。运用了SOCKET套接字实现了很方便的访问TCP/IP协议。多线程的 *** 作。
服务器的程序(简述):
创建socket-->bind()-->listen()-->accept()-->recv/send()-->close()
客户端的程序(简述):
创建scoket-->发送connect-->recv/send()-->close()
这样:
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <pthread.h>
#define MAXLINE 100
void *threadsend(void *vargp)
void *threadrecv(void *vargp)
int main()
{
int *clientfdp
clientfdp = (int *)malloc(sizeof(int))
*clientfdp = socket(AF_INET,SOCK_STREAM,0)
struct sockaddr_in serveraddr
struct hostent *hp
bzero((char *)&serveraddr,sizeof(serveraddr))
serveraddr.sin_family = AF_INET
serveraddr.sin_port = htons(15636)
serveraddr.sin_addr.s_addr = inet_addr("127.0.0.1")
if(connect(*clientfdp,(struct sockaddr *)&serveraddr,sizeof(serveraddr)) <0){
printf("connect error\n")
exit(1)
}
pthread_t tid1,tid2
printf("connected\n")
while(1){
pthread_create(&tid1,NULL,threadsend,clientfdp)
pthread_create(&tid2,NULL,threadrecv,clientfdp)
}
return EXIT_SUCCESS
}
void *threadsend(void * vargp)
{
//pthread_t tid2
int connfd = *((int *)vargp)
int idata
char temp[100]
while(1){
//printf("me: \n ")
fgets(temp,100,stdin)
send(connfd,temp,100,0)
printf(" client send OK\n")
}
printf("client send\n")
return NULL
}
void *threadrecv(void *vargp)
{
char temp[100]
int connfd = *((int *)vargp)
while(1){
int idata = 0
idata = recv(connfd,temp,100,0)
if(idata >0){
printf("server :\n%s\n",temp)
}
}
return NULL
}
扩展资料:注意事项
linux下编译多线程代码时,shell提示找不到 pthread_create函数,原因是 pthread.h不是linux系统默认加载的库文件,应该使用类似如下gcc命令进行编译:
gcc echoserver.c -lpthread -o echoserver
只要注意 -lpthread参数就可以了。
/*服务器*/#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#define SERVPORT 5555
#define BACKLOG 10
#define MAX(a,b) ((a)>(b)?(a):(b))
void str_echo(int)
void server()
{
int sockfd,client_fd
struct sockaddr_in my_addr
struct sockaddr_in remote_addr
socklen_t sin_size
if((sockfd=socket(AF_INET,SOCK_STREAM,0))== -1)
{
perror("socket create error!")
exit(1)
}
my_addr.sin_family=AF_INET
my_addr.sin_port=htons(SERVPORT)
my_addr.sin_addr.s_addr=INADDR_ANY
bzero(&(my_addr.sin_zero),8)
if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))==-1)
{
perror("bind error!")
exit(1)
}
if(listen(sockfd,BACKLOG)==-1)
{
perror("listen error!")
exit(1)
}
while(1)
{
sin_size=sizeof(struct sockaddr_in)
if((client_fd=accept(sockfd,(struct sockaddr *)&remote_addr,&sin_size))==-1)
{
perror("accept error!")
continue
}
printf("received a connection from %s\n",inet_ntoa(remote_addr.sin_addr))
if(!fork())
{
close(sockfd)
str_echo(client_fd)
exit(0)
}
close(client_fd)
}
}
void str_echo(int sockfd)
{
int maxfd
char sendbuf[1024]={0}
char recvbuf[1024]={0}
fd_set rfds
while(1)
{
FD_SET(0,&rfds)
FD_SET(sockfd,&rfds)
maxfd=MAX(0,sockfd)+1
select(maxfd,&rfds,NULL,NULL,NULL)
if(FD_ISSET(sockfd,&rfds))
{
bzero(recvbuf,1024)
recv(sockfd,recvbuf,1024,0)
printf("C:%s\n",recvbuf)
}
if(FD_ISSET(0,&rfds))
{
read(0,sendbuf,1024)
send(sockfd,sendbuf,strlen(sendbuf),0)
bzero(sendbuf,1024)
}
}
}
int main()
{
server()
return 0
}
/*客户端*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define SERVPORT 5555
#define MAXSIZE 1024
#define MAX(a,b) ((a)>(b)?(a):(b))
void client(int argc,char **argv)
{
int sockfd,maxfd
char sendbuf[MAXSIZE]={0}
char recvbuf[MAXSIZE]={0}
// struct hostent *host
struct sockaddr_in serv_addr
fd_set rfds
/* if(argc<2)
{
fprintf(stderr,"Please enter the sserver's name!\n")
exit(1)
}
if((host=gethostbyname(argv[1]))==NULL)
{
herror("get host by name error!\n")
exit(1)
}
*/
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket creat error!\n")
exit(1)
}
serv_addr.sin_family=AF_INET
serv_addr.sin_port=htons(SERVPORT)
// serv_addr.sin_addr=*((struct in_addr *)host->h_addr)
serv_addr.sin_addr.s_addr=inet_addr("127.0.0.1")
bzero(&(serv_addr.sin_zero),8)
if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(struct sockaddr))==-1)
{
perror("connection error!\n")
exit(1)
}
FD_ZERO(&rfds)
while(1)
{
FD_SET(0,&rfds)
FD_SET(sockfd,&rfds)
maxfd=MAX(0,sockfd)+1
select(maxfd,&rfds,NULL,NULL,NULL)
if(FD_ISSET(sockfd,&rfds))
{
bzero(recvbuf,MAXSIZE)
recv(sockfd,recvbuf,MAXSIZE,0)
printf("S:%s\n",recvbuf)
}
if(FD_ISSET(0,&rfds))
{
fgets(sendbuf,MAXSIZE,stdin)
send(sockfd,sendbuf,strlen(sendbuf),0)
bzero(sendbuf,MAXSIZE)
}
}
// close(sockfd)
}
int main(int argc,char **argv)
{
client(argc,argv)
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)