
linux下socket 编程(客户端向服务器发送文件) 求源代码……大哥大姐帮帮忙!! 谢谢 10
展开全部
我用BSD Socket发图片的,很简单。
推荐读物:
“BSD Sockets Programming from a Multi-Language Perspective”
void* streamServer(void* arg)
{
struct sockaddr_in server;
/* make this thread cancellable using pthread_cancel() */
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
/* open socket */
if ((serversock = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
quit("socket() failed", 1);
}
/* setup server's IP and port */
memset(&server, 0, sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
server.sin_addr.s_addr = INADDR_ANY;
/* bind the socket */
if (bind(serversock, (const sockaddr*)&server, sizeof(server)) == -1) {
quit("bind() failed", 1);
}
/* wait for connection */
if (listen(serversock, 10) == -1) {
quit("listen() failed.", 1);
}
/* accept a client */
if ((clientsock = accept(serversock, NULL, NULL)) == -1) {
quit("accept() failed", 1);
}
/* the size of the data to be sent */
int imgsize = img1->imageSize;
int bytes, i;
/* start sending images */
while(1)
{
/* send the grayscaled frame, thread safe */
pthread_mutex_lock(&mutex);
if (is_data_ready) {
bytes = send(clientsock, img1->imageData, imgsize, 0);
is_data_ready = 0;
}
pthread_mutex_unlock(&mutex);
/* if something went wrong, restart the connection */
if (bytes != imgsize) {
fprintf(stderr, "Connection closed.\n");
close(clientsock);
if ((clientsock = accept(serversock, NULL, NULL)) == -1) {
quit("accept() failed", 1);
}
}
/* have we terminated yet? */
pthread_testcancel();
/* no, take a rest for a while */
usleep(1000);
}
}
推荐读物:
“BSD Sockets Programming from a Multi-Language Perspective”
void* streamServer(void* arg)
{
struct sockaddr_in server;
/* make this thread cancellable using pthread_cancel() */
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
/* open socket */
if ((serversock = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
quit("socket() failed", 1);
}
/* setup server's IP and port */
memset(&server, 0, sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
server.sin_addr.s_addr = INADDR_ANY;
/* bind the socket */
if (bind(serversock, (const sockaddr*)&server, sizeof(server)) == -1) {
quit("bind() failed", 1);
}
/* wait for connection */
if (listen(serversock, 10) == -1) {
quit("listen() failed.", 1);
}
/* accept a client */
if ((clientsock = accept(serversock, NULL, NULL)) == -1) {
quit("accept() failed", 1);
}
/* the size of the data to be sent */
int imgsize = img1->imageSize;
int bytes, i;
/* start sending images */
while(1)
{
/* send the grayscaled frame, thread safe */
pthread_mutex_lock(&mutex);
if (is_data_ready) {
bytes = send(clientsock, img1->imageData, imgsize, 0);
is_data_ready = 0;
}
pthread_mutex_unlock(&mutex);
/* if something went wrong, restart the connection */
if (bytes != imgsize) {
fprintf(stderr, "Connection closed.\n");
close(clientsock);
if ((clientsock = accept(serversock, NULL, NULL)) == -1) {
quit("accept() failed", 1);
}
}
/* have we terminated yet? */
pthread_testcancel();
/* no, take a rest for a while */
usleep(1000);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询