检查此项:
服务器C
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define PORT 2024
extern int errno;
int main (){
struct sockaddr_in server;
struct sockaddr_in from;
int sd;
sd = socket (AF_INET, SOCK_STREAM, 0);
bzero (&server, sizeof (server));
bzero (&from, sizeof (from));
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl (INADDR_ANY);
server.sin_port = htons (PORT);
if (bind (sd, (struct sockaddr *) &server, sizeof (struct sockaddr)) == -1){
perror ("bind(). error\n");
return errno;
}
if (listen (sd, 5) == -1){
perror ("listen(). error\n");
return errno;
}
while (1){
int client;
int length = sizeof (from);
printf ("connect to port : %d...\n",PORT);
fflush (stdout);
client = accept (sd, (struct sockaddr *) &from, &length);
if (client < 0)
continue;
char file[100];
read (client, file, 100) <= 0;
printf("Filename : %s\n\n", file);
//
FILE *fl = fopen(file, "r");
char text[500]; //your file size
int len = 0;
char c;
while((c = fgetc(fl)) != EOF){
text[len] = c;
len++;
}
text[len] = '\0';
//printf("%d", len);
//printf("%s", text);
write(client, &len, 4);
write(client, text, len);
close (client);
}
}
客户C
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <string.h>
extern int errno;
int port;
int main (int argc, char *argv[]){
int sd;
struct sockaddr_in server;
if (argc != 3){
printf ("%s <server_adress> <port>\n", argv[0]);
return -1;
}
port = atoi (argv[2]);
if ((sd = socket (AF_INET, SOCK_STREAM, 0)) == -1){
perror ("socket() error.\n");
return errno;
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr(argv[1]);
server.sin_port = htons (port);
if (connect (sd, (struct sockaddr *) &server,sizeof (struct sockaddr)) == -1){
perror ("connect(). error\n");
return errno;
}
char file[100];
printf("Give me filename: ");
fflush(stdout);
scanf("%s", file);
if (write (sd, file, 100) <= 0)
{
perror ("[client]Eroare la write() spre server.\n");
return errno;
}
int len;
char text[500];
read(sd, &len, 4);
read(sd, text, len);
//printf("%s\n", text);
FILE *fl = fopen("newFile.txt", "w");
fprintf(fl, "%s", text);
fclose(fl);
close (sd);
}
在这里,客户机给服务器一个文件名,它(服务器)打开文件并从中读取所有内容,将数据发送给客户机,客户机创建一个新文件并将数据写入新创建的文件中。
UPD。
concurentServer。C
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <pthread.h>
#define PORT 2908
extern int errno;
typedef struct thData{
int idThread;
int cl;
}thData;
static void *treat(void *);
void answer(void *);
int main (){
struct sockaddr_in server;
struct sockaddr_in from;
int nr;
int sd;
int pid;
pthread_t th[100];
int i=0;
if ((sd = socket (AF_INET, SOCK_STREAM, 0)) == -1){
perror ("[server]socket().\n");
return errno;
}
int on=1;
setsockopt(sd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));
bzero (&server, sizeof (server));
bzero (&from, sizeof (from));
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl (INADDR_ANY);
server.sin_port = htons (PORT);
if (bind (sd, (struct sockaddr *) &server, sizeof (struct sockaddr)) == -1) {
perror ("[server]bind().\n");
return errno;
}
if (listen (sd, 2) == -1){
perror ("[server]listen().\n");
return errno;
}
while (1){
int client;
thData * td;
int length = sizeof (from);
printf ("[server]Port : %d...\n",PORT);
fflush (stdout);
if ( (client = accept (sd, (struct sockaddr *) &from, &length)) < 0){
perror ("[server]accept().\n");
continue;
}
int idThread;
int cl;
td=(struct thData*)malloc(sizeof(struct thData));
td->idThread=i++;
td->cl=client;
pthread_create(&th[i], NULL, &treat, td);
}
};
static void *treat(void * arg)
{
struct thData tdL;
tdL= *((struct thData*)arg);
printf ("[thread]- %d - Waiting for message...\n", tdL.idThread);
fflush (stdout);
pthread_detach(pthread_self());
answer((struct thData*)arg);
close ((intptr_t)arg);
return(NULL);
};
void answer(void *arg){
char file[100];
int ap;
struct thData tdL;
tdL= *((struct thData*)arg);
//
if (read (tdL.cl, file, 100) <= 0){
printf("[Thread number : %d]\n",tdL.idThread);
}
FILE *fl = fopen(file, "r");
char text[500]; //your file size
int len = 0;
char c;
while((c = fgetc(fl)) != EOF){
text[len] = c;
len++;
}
text[len] = '\0';
//printf("%d", len);
//printf("%s", text);
write(tdL.cl, &len, 4);
write(tdL.cl, text, len);
}