想在Linux下用socket套接字写个类似于windows下的QQ聊天程序,但是遇到不能循环发送和接受的问题,希望能向各位大侠请教,以下是代码:
这是server端代码:
I assume you can make sense of my code!
so i just show you code below,if you have any question,
just let me know !!1
server.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#define BUF_SIZE 1024
#define LISTEN_PORT 8889
#define LISTEN_NO 10
#define err_exit(msg) (perror(msg),(exit(EXIT_FAILURE)))
int sock_make(void);
void read_from(int fd);
void send_to(int fd);
int main(void)
{
int sockfd,connfd;
struct sockaddr_in clientaddr;
int n;
char buf[BUF_SIZE];
char recvbuf[BUF_SIZE];
char sendbuf[BUF_SIZE];
sockfd=sock_make();
while(1){
//If you wanna know which client and what the ip is
//You should no set the the argu being NULL ..
if((connfd=accept(sockfd,(struct sockaddr*)NULL,NULL))<0)
{
err_exit(">>accept");
}else{
while(1){
puts("-----------wait message---------");
KEEP_READ:
n=read(connfd,recvbuf,sizeof(recvbuf));
//recvbuf[n]=0;
printf("%s\n",recvbuf);
if(strncmp(recvbuf,".",1)) goto KEEP_READ;
puts("-----------Please input----------");
KEEP_SEND:
n=read(STDIN_FILENO,sendbuf,sizeof(sendbuf));
sendbuf[n]=0;
write(connfd,sendbuf,sizeof(sendbuf));
if(strncmp(sendbuf,".",1)) goto KEEP_SEND;
}
close(connfd);
}
}
close(sockfd);
return 0;
}
int sock_make(void)
{
struct sockaddr_in clientaddr;
int sockfd;
memset(&clientaddr,SOCK_STREAM,sizeof(clientaddr));
clientaddr.sin_family=AF_INET;
clientaddr.sin_port=htons(LISTEN_PORT);
clientaddr.sin_addr.s_addr=htonl(INADDR_ANY);
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
err_exit(">>socket");
}
if(bind(sockfd,(struct sockaddr*)&clientaddr,sizeof(clientaddr))<0)
{
err_exit(">>bind");
}
listen(sockfd,LISTEN_NO);
return sockfd;
}
client.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#define BUF_SIZE 1024
#define LISTEN_PORT 8889
#define LISTEN_NO 10
#define err_exit(msg) (perror(msg),(exit(EXIT_FAILURE)))
int main(int argc,char * argv[])
{
int sockfd;
int n;
struct sockaddr_in serverAddr;
char sendbuf[BUF_SIZE];
char recvbuf[BUF_SIZE];
if((sockfd=socket(PF_INET,SOCK_STREAM,0))<0)
{
err_exit(">>socket");
}
memset(&serverAddr,0,sizeof(serverAddr));
serverAddr.sin_family=AF_INET;
serverAddr.sin_port=htons(LISTEN_PORT);
inet_pton(PF_INET,argv[1],&serverAddr.sin_addr.s_addr);
while(1)
{
if((connect(sockfd,(struct sockaddr*)&serverAddr,sizeof(serverAddr)))<0)
{
err_exit(">>connect");
}else{
while(1){
puts("--------------Please input-----------");
KEEP_SEND:
n=read(STDIN_FILENO,sendbuf,sizeof(sendbuf));
sendbuf[n]=0;
write(sockfd,sendbuf,sizeof(sendbuf));
if(strncmp(sendbuf,".",1)) goto KEEP_SEND;
puts("-------------Wait message------------");
KEEP_READ:
n=read(sockfd,recvbuf,sizeof(recvbuf));
//recvbuf[n]=0; //EOF ..
printf("%s\n",recvbuf);
if(strncmp(recvbuf,".",1)) goto KEEP_READ;
}
close(sockfd);
}
}
return 0;
}
Please ,run the client as :
./client 127.0.0.1
if you run the both on both terminal
and you see something below:
terminal 1 (run server)
landpack@landpack-VirtualBox:~/ak/csdn$ ./server
-----------wait message---------
hello
I am Landpack
.
-----------Please input----------
Hey Landpack !
good to know you
.
-----------wait message---------
terminal 2 (run client)
landpack@landpack-VirtualBox:~/ak/csdn$ ./client 127.0.0.1
--------------Please input-----------
hello
I am Landpack
.
-------------Wait message------------
Hey Landpack !
good to know you
.
--------------Please input-----------
All suggestion are welcome !
Have fun !!
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。