当前位置: 技术问答>linux和unix
Linux下sockek程序每次傳輸圖片都不完整****(順便好奇怪,怎麼打出繁體字)
来源: 互联网 发布时间:2016-11-19
本文导语: 本帖最后由 Sasoritattoo 于 2011-02-12 16:41:53 编辑 好奇怪,都沒有安裝繁體字的字庫什麽的,怎麼會打出繁體字呢???真奇怪。。。。。 -----------------------------------看不见我--------------------------------------------------- ------...
-----------------------------------看不见我---------------------------------------------------
-----------------您奇怪完了之後,請看下面的問題吧------------------------------------------------
-----------------------------------看不见我---------------------------------------------------
我最近在搞Linux下的網絡傳輸問題,邊搞邊學嘛。。。。(哎呀,似乎繁體字看起來很麻煩呢,見諒啊大家)
服務器端運行在虛擬機中的Linux下,客戶端運行在arm板上Linux系統中,從客戶端傳輸一張圖片到虛擬機Linux中
-----------------------------------看不见我---------------------------------------------------
-------------我試過了,好像打出繁體字似乎跟chrome瀏覽器有關,换了ie浏览器,下面使用简体字------------
-----------------------------------看不见我---------------------------------------------------
下面上一下代码,很简单的,应该大致浏览下就好
/*socket_client.c**********run in arm linux system*/
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 4321
#define BUFFER_SIZE 1024
int main(int argc, char *argv[])
{
int sock_fd,sendbytes;
FILE *file_fd;
int buf[BUFFER_SIZE]; // It is the integer
struct hostent *host;
struct sockaddr_in serv_addr;
long int num;
memset(buf,0,sizeof(buf));
file_fd = fopen("test-picture.jpg","rb");
if(argc h_addr);
bzero(&(serv_addr.sin_zero),8);
if(-1 == connect(sock_fd,(struct sockaddr *)&serv_addr,sizeof(struct sockaddr)))
{
perror("connect");
exit(1);
}
while(1)
{
if(-1 == (num = fread(buf,1,sizeof(buf),file_fd)))
{
perror("fopen");
exit(1);
}
if(0 == num) break;
if(-1 == (sendbytes = send(sock_fd,buf,num,0))) //send()
{
perror("send");
exit(1);
}
if(num != sendbytes)
{
//perror("num != sendbytesn");
printf("num != sendbytesn");
//break;
}
printf("file words num is :%dn",num);
printf("-----sendbytes is :%dnn",sendbytes);
}
close(sock_fd);
close(file_fd);
exit(0);
}
-----------------------------------看不见我---------------------------------------------------
/*socket_server.c********* run in pc linux*/
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 4321 //PORT number must be the same with client
#define BUFFER_SIZE 1024 // 1024 * 1024 = 1048576
#define MAX_QUE_CONN_NM 10
int main()
{
struct sockaddr_in server_sockaddr,client_sockaddr;
int sin_size,recvbytes;
int sock_fd,client_fd;
FILE *file_fd;
char buf[BUFFER_SIZE];
long int num;
int ii;
if(-1 == (sock_fd = socket(AF_INET,SOCK_STREAM,0))) //SOCK_STREAM
{
perror("socket");
exit(1);
}
printf("Socket id = %d(sock_fd)n",sock_fd);
server_sockaddr.sin_family = AF_INET;
server_sockaddr.sin_port = htons(PORT);
server_sockaddr.sin_addr.s_addr = INADDR_ANY;
bzero(&(server_sockaddr.sin_zero),8);
int i = 1;
setsockopt(sock_fd,SOL_SOCKET,SO_REUSEADDR,&i,sizeof(i));
if(-1 == bind(sock_fd,(struct sockaddr *)&server_sockaddr,sizeof(struct sockaddr)))
{
perror("bind");
exit(1);
}
printf("Bind success!n");
if(-1 == listen(sock_fd,MAX_QUE_CONN_NM))
{
perror("listen");
exit(1);
}
printf("Listening....n");
if(-1 == (client_fd = accept(sock_fd,(struct sockaddr *)&client_sockaddr,&sin_size)))
{
perror("accept");
exit(1);
}
file_fd = fopen("./test-picture.jpg","wb");
while(1)
{
memset(buf,0,sizeof(buf));
if(-1 == (recvbytes = recv(client_fd,buf,sizeof(buf),0)))//Yes,from here
{ //Use while cycle
perror("recv");
exit(1);
}
printf("sizeof(buf)= %dn",sizeof(buf));
printf("receive bytes num is :%dnn",recvbytes);
//FILE * abc_fd;
if(-1 == (num = fwrite(buf,sizeof(buf),1,file_fd)))
{
perror("fwrite date ");
exit(1);
} //over here ,write over
if(recvbytes