真的是新来的任何进一步的错误,很抱歉。我有一些新的学校项目(学习C),来为连接到使用套接字不是从服务器下载所有代码行的服务器什么我必须这样做。 从那以后,我需要的线,所以他们是为了,在那里我得到的是对排序排序..好,我已经下载的代码行保存它们结构阵列,但现在我的冒泡排序显示一些错误给我,我不知道有什么问题..任何帮助THX。
typedef struct DATA{
char* buf;
}DATA;
// this fucntion creates a socket.
void sort_array(DATA *to_sort, int len){
int i, j;
char tmp[1024] = "";
for (i = 0; i < len - 1; i++){
for (j = 0; j < len - i - 1; j++){
if (strcmp(to_sort[j].buf, to_sort[j + 1].buf) < 0){
strcpy(tmp, to_sort[j + 1].buf);
strcpy(to_sort[j + 1].buf, to_sort[j].buf);
strcpy(to_sort[j].buf, tmp);
}
}
}
}
int main(){
WSADATA info;
int error, s,j;
int sendError, recvError;
char buffer[1024] = "100",readbuf[1024] = "";
char recvbuf[1024] = "";
int numberLines, i, temp, convert;
char converted_num[1024] = "";
char *sub;
struct sockaddr_in ClientService;
FILE *fp = fopen("stored_data.txt", "w");
FILE *ofp = fopen("final_result.txt", "w");
DATA *to_sort = NULL;
error = WSAStartup(MAKEWORD(2, 0), &info);
//check if error occurred while configuring.
if (error != 0){
printf("WSAstartup failed with error: %d\n", error);
exit(1);
}
s = socket_creation(fp);
// configuration of the socket.
ClientService.sin_family = AF_INET;
ClientService.sin_addr.s_addr = inet_addr("54.209.143.42");
ClientService.sin_port = htons(6714);
connection(s, ClientService, fp); // function connecting to the server.
error = WSAStartup(MAKEWORD(2, 0), &info);
// send '100' login command to server.
strcpy(buffer, "100");
sendError = send_to_serv(buffer, s);
// receiving respond from the server.
recvError = recv_from_serv(s, &numberLines, fp,buffer);
// send '400' get number lines command to server.
strcpy(buffer, "400");
sendError = send_to_serv(buffer, s);
// receiving respond from the server.
recvError = recv_from_serv(s, &numberLines, fp,buffer);
printf("\nNumber of Lines are: %d\n", numberLines);
temp = numberLines; // number of all lines received.
/* allocate mmoery for struct array to store the data from server */
to_sort = (DATA*)malloc(sizeof(DATA)* temp);
// getting the lines from the server.
for (i = 0; i < temp; i++){
j = 0;
convert = 5000001 + i; // creating number of line wanted.
_itoa(convert, converted_num, 10); // converting the int to a string (wanted line).
sendError = send_to_serv(converted_num, s); // sending the server request of line wanted.
recv_from_serv(s, &numberLines, fp, buffer); // receive the line wanted.
sub = substring(buffer, 0, 3);
// checks if the server returned '502 OK' or '501 REJECT'
if (strcmp(sub, "502") != 0){
to_sort[j].buf = buffer;
j++;
}
}
sort_array(to_sort, temp); // sorting the struct array.
// printing the final result.
// clean memoery.
free(to_sort);
fclose(fp);
system("PAUSE>nul");
return 0;
}