#include <stdio.h> #include <stdlib.h> #include <string.h> #include <netinet/in.h> #include <arpa/inet.h> #include <sys/socket.h> #include <sys/types.h> #include <unistd.h> #define PORT 2000 #define BACKLOG 1 /* ********************************************************************************* *function require: a ip pointer , a pointer point data ,data length * *function aimed at: to send data from a computer by tcp * *varible information: * * 1. a pointer point string contain remote computer recieve the data-char * * 2. a pointer point data contain data wanan remote computer-unsigned char* * 3. a int to store the length of the data * * warninng: the send data only once * *function result: 0 well ,1 error * ********************************************************************************* */ int datasend(unsigned char *data, int length) { int socketfd, connectfd; struct sockaddr_in server; struct sockaddr_in client; socklen_t addrlen; int leng = 0; int num = 0; if ((socketfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(-1); } bzero(&server, sizeof(server)); server.sin_family = AF_INET; server.sin_port = htons(PORT); server.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(socketfd, (struct sockaddr *) &server, sizeof(server)) == -1) { perror("bind"); exit(-1); } if (listen(socketfd, BACKLOG) == -1) { perror("listen"); exit(-1); } if ((connectfd = accept(socketfd, (struct sockaddr *) &client, &addrlen)) == -1) { perror("accept"); exit(-1); } printf("You got a connection from client'op is %s , port is %d/n", inet_ntoa(client.sin_addr), htons(client.sin_port)); while (leng < length) { if ((num = send(connectfd, data + leng, length - leng, 0)) == -1) { perror("send data:"); //close(socketfd); return -1; } leng += num; } close(connectfd); close(socketfd); return 0; } int main(int argc ,char * argv[]) { unsigned char data[40]; printf("Please input a data to string ./n"); scanf("%s",data); datasend(data,strlen((char *)data) - 1); printf("hello world !/n"); return 0; } 但是有个问题,好像中间不能存在空格,好纠结啊..... #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <netinet/in.h> #include <unistd.h> #include <sys/types.h> #include <netdb.h> #define PORT 2000 #define MAXSIZE 100 /* ********************************************************************************* *function require: a string head pointer , a data head pointer , a pointer int * *fuction aimed at: to recv data from remote computer * *function work status: 0 well ,1 error * * the require varible list and detail: * * 1. a point string contain the remote computer ip addrsss * * 2.data pointer is container to store the information from remote * * 3.a pointer int to store the data formation length * ********************************************************************************* */ int datarecv( char * ip , unsigned char * data, int *length) { int listenfd , num ; struct hostent * he; struct sockaddr_in server; if ((he = gethostbyname(ip)) == NULL) { perror("gethostbyname"); exit(1); } if ((listenfd = socket(AF_INET,SOCK_STREAM,0)) == -1) { perror("socket"); exit(1); } bzero(&server,sizeof(server)); server.sin_family = AF_INET; server.sin_port = htons(PORT); server.sin_addr = *((struct in_addr *)he->h_addr); if (connect (listenfd,(struct sockaddr *)&server,sizeof(server)) == -1) { perror("connect"); exit(1); } if ((*length = recv(listenfd,data,MAXSIZE,0)) == -1) { perror("recv"); exit(1); } data[*length] = '/0'; printf("server message:%s /n",data); close(listenfd); return 0; } int main(int argc ,char * argv[]) { int length; char data[MAXSIZE]; if (argc != 2) { printf("error./n"); exit(1); } datarecv(argv[1],(unsigned char *)data,&length); return 0; } 我这点技术有点小,请各位指教了!!