使用参数" server"调用程序时然后" runserver"方法正在执行,事情正常。只是printf()
语句没有打印任何内容。但是,当我打电话给" runclient"方法printf
之前工作正常。为什么会这样?怎么解决这个问题?
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <strings.h>
#include <sys/types.h>
#include <netdb.h>
void error(char *msg)
{
perror(msg);
exit(1);
}
int runclient()
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
portno = 1024;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname("localhost");
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
printf("Please enter the message: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer,256);
n = read(sockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
printf("%s\n",buffer);
return 0;
}
int runserver(){
int sockfd, portno, clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
//bzero((char *) &serv_addr, sizeof(serv_addr));
portno = 1024;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sockfd,5);
clilen = sizeof(cli_addr);
sockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (sockfd < 0)
error("ERROR on accept");
//bzero(buffer,256);
n = read(sockfd,buffer,255);
if (n < 0) error("ERROR reading from socket");
printf("Here is the message: %s\n",buffer);
n = write(sockfd,"I got your message",18);
if (n < 0) error("ERROR writing to socket");
return 0;
}
int main(int argc, char **argv)
{
if(strcmp(argv[1],"server") == 0){
printf("Starting Server .... ");
runserver();
} else {
printf("Starting Client .... ");
runclient();
}
return 0;
}
答案 0 :(得分:4)
#!usr/bin/python
import sys
import json
args=sys.stdin.readlines() #args comes in as a list with one item in it
which is the parameter that you sent in from javascript
arguments=args[0]
print "Content-Type: text/html"
print ""
def getTasks(userName):
"""This function will do some processing and get the required return
value"""
taskList=[]
#read JSON file and get the info.
with open("Somefile.json", 'r') as data_file:
usersDictionary = json.load(data_file)
data_file.close()
"""...do some process ing of data here, which will set some data into
the list called taskList"""
return taskList #Return a list of all the tasks for the user.
print json.dumps(getTasks(arguments))
"""convert the json to a string and print it out. what you print out here
will be what you will get as a string in the .responseText of the
XMLHttpRequest
object"""
是行缓冲的,因此您必须在适当的位置拨打stdout
或添加换行符。