- 论坛徽章:
- 0
|
本帖最后由 dingzhou2008 于 2013-09-08 12:52 编辑
下面是两个linux下网络传输程序,server.c和client.c,分别发送和接收照片数据。照片大小32K。编译后在ubuntu下运行正常图片正确,但交叉编译后server在开发板上运行,client在ubuntu下运行,接收到的图片数据能显示,但有错位。请大家帮忙分析一下。
- server.c如下:
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <sys/types.h>
- #include <string.h>
- #include "proto.h"
- #define BUFSIZE 128 //1024
- #define STRSIZE 40
- static void server_job(int sd) {
- char buf[BUFSIZE];
- int len, i, count;
- char jpgbuf[1024 * 500];
- char statebuf[40];
- FILE *input_file;
- if ((input_file = fopen("1.jpg", "rb")) == NULL) {
- fprintf(stderr, "can't open 1.jpg\n");
- return;
- }
- fread(jpgbuf, 32768, 1, input_file);
- for (count = 0; count < 32; count++) {
- if (send(sd, jpgbuf+count*1024, 1024, 0) < 0) {
- printf("Send File:\t JPEG picture Failed\n");
- }
- }
- }
- int main() {
- char ipstr[STRSIZE];
- int sd, newsd;
- struct sockaddr_in laddr, raddr;
- socklen_t raddr_len;
- pid_t pid;
- sd = socket(AF_INET, SOCK_STREAM, 0/*IPPROTO_TCP,IPPROTO_SCTP*/);
- if (sd < 0) {
- perror("socket()");
- exit(1);
- }
- int val = 1;
- if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0) {
- perror("setsockopt(SO_REUSEADDR)");
- }
- laddr.sin_family = AF_INET;
- laddr.sin_port = htons(atoi(SERVERPORT));
- inet_pton(AF_INET, "0.0.0.0", &laddr.sin_addr);
- if (bind(sd, (void *) &laddr, sizeof(laddr)) < 0) {
- perror("bind()");
- exit(1);
- }
- if (listen(sd, 200) < 0) {
- perror("listen()");
- exit(1);
- }
- raddr_len = sizeof(raddr); /*!!!!!!!!*/
- while (1) {
- newsd = accept(sd, (void *) &raddr, &raddr_len);
- if (newsd < 0) {
- perror("accept()");
- exit(1);
- }
- pid = fork();
- if (pid < 0) {
- fprintf(stderr, "fork()");
- exit(1);
- }
- if (pid == 0) {
- close(sd);
- inet_ntop(AF_INET, &raddr.sin_addr, ipstr, STRSIZE);
- printf("Client:%s:%d\n", ipstr, ntohs(raddr.sin_port));
- server_job(newsd);
- close(newsd);
- exit(1);
- }
- close(newsd);
- wait(NULL);
- }
- exit(0);
- }
- client.c如下:
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include "proto.h"
- /* Address to accept any incoming messages. */
- #define INADDR_ANY ((in_addr_t) 0x00000000)
- #define BUFFER_SIZE 1024
- /* Set N bytes of S to 0. */
- extern void bzero(void *__s, size_t __n)
- __THROW __nonnull ((1));
- char recieveBuffer[BUFFER_SIZE];
- int leftRecieveChars;
- int currentIndex = 0;
- int sd;
- char getOneChar() {
- currentIndex++;
- if (leftRecieveChars == 0) {
- bzero(recieveBuffer, BUFFER_SIZE);
- do {
- leftRecieveChars = recv(sd, recieveBuffer, BUFFER_SIZE, 0);
- if (leftRecieveChars < 0) {
- printf("Server Recieve Data Failed!\n");
- exit(1);
- }
- } while (leftRecieveChars == 0);
- currentIndex = 0;
- }
- leftRecieveChars--;
- return recieveBuffer[currentIndex];
- }
- int main(int argc, char *argv[]) {
- FILE *fp;
- int i, jpegSize;
- long long stamp;
- struct sockaddr_in raddr;
- char numberBuf[20];
- if (argc < 2) {
- fprintf(stderr, "Usage...\n");
- exit(1);
- }
- //创建用于internet的流协议(TCP)socket,用client_socket代表客户机socket
- sd = socket(AF_INET, SOCK_STREAM, 0);
- if (sd < 0) {
- perror("socket()");
- exit(1);
- }
- //设置一个socket地址结构client_addr,代表客户机internet地址, 端口
- struct sockaddr_in client_addr;
- bzero(&client_addr, sizeof(client_addr)); //把一段内存区的内容全部设置为0
- client_addr.sin_family = AF_INET; //internet协议族
- client_addr.sin_addr.s_addr = htons(INADDR_ANY); //INADDR_ANY表示自动获取本机地址
- client_addr.sin_port = htons(0); //0表示让系统自动分配一个空闲端口
- //把客户机的socket和客户机的socket地址结构联系起来
- if (bind(sd, (struct sockaddr*) &client_addr, sizeof(client_addr))) {
- printf("Client Bind Port Failed!\n");
- exit(1);
- }
- raddr.sin_family = AF_INET;
- raddr.sin_port = htons(atoi(SERVERPORT));
- inet_pton(AF_INET, argv[1], &raddr.sin_addr);
- if (connect(sd, (void *) &raddr, sizeof(raddr)) < 0) {
- perror("connect()");
- exit(1);
- }
- int count = 0, cnt = 0;
- char name[20];
- //while (1) {
- printf("Begin recieve data.the num is %d!\n", cnt);
- cnt++;
- jpegSize = 32768;
- char fileBuf[jpegSize];
- bzero(fileBuf, jpegSize);
- //printf("Begin receive JPG date !\n");
- for (count = 0; count < 32; count++) {
- leftRecieveChars = recv(sd, fileBuf + count * 1024, 1024, 0);
- }
- sprintf(name, "%d.jpg", count);
- count++;
- if (count >= 7)
- count = 0;
- fp = fopen(name, "w");
- int writeLength = fwrite(fileBuf, sizeof(char), 32768, fp);
- //printf("writeLength to file : %d\n", writeLength);
- if (writeLength < 32768) {
- printf("File:\t%s Write Failed\n", "1.jpeg");
- fclose(fp);
- exit(1);
- }
- fclose(fp);
- return 0;
- }
复制代码 |
|