linux tcp server client

来源:互联网 发布:php抓取网页数据 编辑:程序博客网 时间:2024/05/01 09:02

server

-------------------------------------------------------

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>    
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdlib.h>
#include<errno.h>
#include<string.h>

int port = 8000;

int main()
{
 struct sockaddr_in sin;
 struct sockaddr_in pin;
 int sock_descriptor;
 int lenTotal;
 int temp_sock_descriptor;
 socklen_t address_size;
 char buf[1024];
 int i, len;
 //FILE *fp=fopen("/home/truman/updata.rar", "wb");
 FILE *fp;
 sock_descriptor = socket(AF_INET, SOCK_STREAM, 0);
 if (sock_descriptor == -1){
  perror("call to socket");
  exit(1);
 }
 bzero(&sin, sizeof(sin));
 sin.sin_family = AF_INET;
 sin.sin_addr.s_addr = INADDR_ANY;
 sin.sin_port = htons(port);
 address_size=sizeof(pin);
 if (bind(sock_descriptor, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
  perror("call to bind");
  exit(1);
 }
 if (listen(sock_descriptor, 20) == -1) {
  perror("call to listen");
  exit(1);
 }
 printf("Accepting connections .../n");
 while(1){
  temp_sock_descriptor = accept(sock_descriptor, (struct sockaddr *)&pin,&address_size);
  if (temp_sock_descriptor == -1){
   perror("call to accept");
   exit(1);
  }
  fp = fopen("b.txt", "wb");
  if (fp == NULL){
   printf("file updata.rar open failed!/n");
   exit(1);
  }
  lenTotal = 0;
  while((len = recv(temp_sock_descriptor,buf,sizeof(buf),0)) > 0){//接收服务器传送的文件
   lenTotal += len;
   fwrite(buf,sizeof(char),len,fp);//将以iobuffer为首地址的数据块写到文件中
   bzero(buf,1024);  
  }
  printf("recv len:%d/n",lenTotal);
  printf("save file success!/n");
  fclose(fp);
  close(temp_sock_descriptor);
 }
 return(0);
}
client

------------------------------------------------------------------

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>    
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdlib.h>
#include<errno.h>
#include<string.h>


char * host_name = "192.168.58.128"; // local host
int port = 8000;

int main(int argc, char *argv[])
{
 char buf[1024];
 char message[256];
 int len;
 int lenTotal;
 int sd;
 int fd;
 FILE * fp;
 struct sockaddr_in pin;
 struct hostent *nlp_host;
 if (argc != 2)          //为输入文件则给出提示
 {
  printf("Usage: %s filename/n",argv[0]);
  exit(1);
 }
 fp = fopen(argv[1],"rb");
 if(NULL == fp )
 {
  printf("File:/t%s Can Not Open To Read/n", argv[1]);
  exit(1);
 }
 if ((nlp_host = gethostbyname(host_name)) == 0) {
  printf("Error resolving local host/n");
  exit(1);
 }
 bzero(&pin, sizeof(pin));
 pin.sin_family = AF_INET;
 pin.sin_addr.s_addr = htonl(INADDR_ANY);
 pin.sin_addr.s_addr = ((struct in_addr *)(nlp_host->h_addr))->s_addr;
 pin.sin_port = htons(port);
 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  printf("Error opening socket/n");
  exit(1);
 }
 if (connect(sd, (void *)&pin, sizeof(pin)) == -1) {     //跟主机建立Tcp连接
  printf("Error connecting to socket/n");
  exit(1);
 }
 lenTotal = 0;
 while((len = fread(buf,sizeof(char),1024,fp))>0){//发送文件
  lenTotal += len;
  send(sd,buf,len,0);
 }
 printf("send file success!send len = %d/n",lenTotal);
 fclose(fp);
 close(sd);
 return(0);
}

原创粉丝点击