
求Linux下编写TCP服务器中接受并存储图片的代码。高分 80
小弟只会写接收文本执行低级算法的。但是要求的是:获得键盘输入(比如:命令p)----发送给客户端接收客户端的照片(read--图片320*240*3+54)-----获取...
小弟只会写接收文本执行低级算法的。但是要求的是:获得键盘输入 (比如:命令p)----发送给客户端接收客户端的照片(read--图片 320*240*3+54)----- 获取时间保存图片(open文件并创建,写图片数据,close)。以下是小弟洗的接收文本执行低级算法的代码。求大神给个几首并存储图片的。
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main()
{
int listen_fd = socket(PF_INET, SOCK_STREAM, 0);
if(-1 == listen_fd)
{
perror("socket");
return -1;
}
struct sockaddr_in myaddr = {0}, clientaddr = {0};
myaddr.sin_family = AF_INET;
myaddr.sin_port = 8888;
myaddr.sin_addr.s_addr = inet_addr("0.0.0.0");
int len = sizeof myaddr;
if(-1 == bind(listen_fd, (struct sockaddr *)&myaddr, len))
{
perror("bind");
return -1;
}
if(-1 == listen(listen_fd, 10))
{
perror("listen");
return -1;
}
int client_fd = accept(listen_fd, (struct sockaddr *)&clientaddr, &len);
if(-1 == client_fd)
{
perror("accept");
return -1;
}
printf("connected: %s\n", inet_ntoa(clientaddr.sin_addr) );
char buf[100];
while(1)
{ int a ;int b;int c;
read(client_fd, buf, 100);
a = ((int)buf[0]-48);
c = ((int)buf[2]-48);
if (buf[1]=='+')
{
b=a+c;
}
if(buf[1]=='-')
{
b=a-c;
}
if(buf[1]=='*')
{
b=a*c;
}
if(buf[1]=='/')
{
b=a/c;
}
buf[0]=((char)(b/10+48));
buf[1]=((char)(b%10+48));
buf[2]='\0';
write(client_fd, buf, 100);
}
} 展开
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main()
{
int listen_fd = socket(PF_INET, SOCK_STREAM, 0);
if(-1 == listen_fd)
{
perror("socket");
return -1;
}
struct sockaddr_in myaddr = {0}, clientaddr = {0};
myaddr.sin_family = AF_INET;
myaddr.sin_port = 8888;
myaddr.sin_addr.s_addr = inet_addr("0.0.0.0");
int len = sizeof myaddr;
if(-1 == bind(listen_fd, (struct sockaddr *)&myaddr, len))
{
perror("bind");
return -1;
}
if(-1 == listen(listen_fd, 10))
{
perror("listen");
return -1;
}
int client_fd = accept(listen_fd, (struct sockaddr *)&clientaddr, &len);
if(-1 == client_fd)
{
perror("accept");
return -1;
}
printf("connected: %s\n", inet_ntoa(clientaddr.sin_addr) );
char buf[100];
while(1)
{ int a ;int b;int c;
read(client_fd, buf, 100);
a = ((int)buf[0]-48);
c = ((int)buf[2]-48);
if (buf[1]=='+')
{
b=a+c;
}
if(buf[1]=='-')
{
b=a-c;
}
if(buf[1]=='*')
{
b=a*c;
}
if(buf[1]=='/')
{
b=a/c;
}
buf[0]=((char)(b/10+48));
buf[1]=((char)(b%10+48));
buf[2]='\0';
write(client_fd, buf, 100);
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询