LINUX网络编程TCP的服务器 客户端 有乱码怎么解决?
- 提问者网友:抽煙菂渘情少年
- 2021-07-19 04:00
#include <stdio.h>#include <stdlib.h>#include <string.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h>
#define SA struct sockaddr
int main(int argc, char **argv){ int socketfd,n,clilen; struct sockaddr_in server; char buff[1024];
if(argc<2) { printf("usage:a.out IP port\n"); exit(0); } if((socketfd = socket(AF_INET,SOCK_STREAM,0))<0) { printf("socket creation error...\n"); exit(0); }
bzero(&server,sizeof(server)); server.sin_family = AF_INET; inet_pton(AF_INET,argv[1],&server.sin_addr); server.sin_port = htons(8889);
if((n=connect(socketfd,(SA *)&server,sizeof(server)))<0) { printf("connect error...\n"); exit(0); } while(fgets(buff,1024,stdin)!=NULL) { write(socketfd,buff,strlen(buff)); n=read(socketfd,buff,1023); buff[n+1]+='\0'; printf("The client received message is: %s\n",buff); } close(socketfd); }
服务器端代码如下:#include <stdio.h>#include <stdlib.h>#include <string.h> #include <sys/socket.h> #include <netinet/in.h>
#define SA struct sockaddr
int main(int argc, char **argv){ int listenfd,connectfd,n,clilen; struct sockaddr_in server,client; char buff[1024];
if((listenfd = socket(AF_INET,SOCK_STREAM,0))<0) { printf("socket creation error...\n"); exit(0); }
bzero(&server,sizeof(server)); server.sin_family = AF_INET; server.sin_addr.s_addr = htonl(INADDR_ANY); server.sin_port = htons(8889);
if((n=bind(listenfd,(SA *)&server,sizeof(server)))<0) { printf("bind error...\n"); exit(0); }
if((n=listen(listenfd,5))<0){ printf("Listen error...\n"); exit(0); } for(;;){ clilen=sizeof(client); if((connectfd=accept(listenfd,(SA *)&client,&clilen))<0) { printf("Accept error...\n"); exit(0); }
while((n=read(connectfd,buff,1023))>0) { buff[n+1]+='\0'; printf("The received message is: %s\n",buff); write(connectfd,buff,1023); } close(connectfd); }}
在LINUX先运行服务器端,再用客户端连接,能运行但是出现乱码,请问下怎么解决?(好像用fputs 什么什么的)
- 五星知识达人网友:轮獄道
- 2021-07-19 04:50
客户端代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define SA struct sockaddr
#define MAXLINE 1024
int
main(int argc, char **argv)
{
int sockfd;
struct sockaddr_in servaddr;
if (argc != 2) printf("usage: udpcli <IPaddress>");
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(8889);
//Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
dg_cli(stdin, sockfd, (SA *) &servaddr, sizeof(servaddr));
exit(0);
}
int
dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen)
{
int n;
char sendline[MAXLINE], recvline[MAXLINE + 1];
while (fgets(sendline, MAXLINE, fp) != NULL) {
printf("the client received the message is:\n");
sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen);
n = recvfrom(sockfd, recvline, MAXLINE, 0, NULL, NULL);
recvline[n] = '\0';
fputs(recvline, stdout);
}
}
服务器代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define SA struct sockaddr
#define MAXLINE 1023
int main(int argc, char** argv)
{ int sockfd;
struct sockaddr_in servaddr, cliaddr;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(8889);
bind(sockfd, (SA *) &servaddr, sizeof(servaddr));
dg_echo(sockfd, (SA *) &cliaddr, sizeof(cliaddr));
return 0;
}
int
dg_echo(int sockfd, SA *pcliaddr, socklen_t clilen)
{
int n;
socklen_t len;
char mesg[MAXLINE];
for ( ; ; ) {
len = clilen;
printf("the server received the message is:\n");
while((n = recvfrom(sockfd, mesg, MAXLINE, 0, pcliaddr, &len))>0) {
mesg[n] ='\0';
fputs(mesg, stdout);
sendto(sockfd, mesg, n, 0, pcliaddr, len);
}
}
}
你拿去运行,运行得了你一定要把分给我啊,我们刚刚做完这个实验!
- 1楼网友:英雄的欲望
- 2021-07-19 06:16
buff[n+1]+='\0';
这行不对吧?
buff[n]='\0';
应该就可以了。