linux下的 socket编程问题!
linux\u7684socket\u7f16\u7a0b\u95ee\u9898\u5148\u8981\u5f04\u6e05\u695a\uff1a\u662fsocket\u901a\u4fe1\u5ef6\u65f6\uff1f\u8fd8\u662f\u754c\u9762\u5237\u65b0\u5ef6\u65f6\uff1f
\u591a\u7ebf\u7a0b\u65b9\u5f0f\u4e0b\uff0csocket\u5ef6\u65f6\u7684\u8bdd\uff1a\u770b\u7f51\u7edc\u662f\u5426\u4e0d\u6b63\u5e38\uff1f\u53d1\u9001\u6570\u636e\u91cf\u662f\u5426\u8fc7\u5927\uff1fserver\u7aef\u7684recv()\u63a5\u6536\u65b9\u5f0f\u662f\u5426\u9020\u6210\u5ef6\u65f6\uff1f
\u3000\u3000\u4e0b\u9762\u5927\u6982\u5206\u51e0\u4e2a\u65b9\u9762\u8fdb\u884c\u7f57\u5217\uff1a
\u3000\u3000Linux\u8981\u5305\u542b
\u3000\u3000[cpp]
\u3000\u3000#include
\u3000\u3000#include
\u3000\u3000#include
\u3000\u3000#include
\u3000\u3000\u7b49\u5934\u6587\u4ef6\uff0c\u800cwindows\u4e0b\u5219\u662f\u5305\u542b
\u3000\u3000[cpp]
\u3000\u3000#include
\u3000\u3000\u3002
\u3000\u3000Linux\u4e2dsocket\u4e3a\u6574\u5f62\uff0cWindows\u4e2d\u4e3a\u4e00\u4e2aSOCKET\u3002
\u3000\u3000Linux\u4e2d\u5173\u95edsocket\u4e3aclose\uff0cWindows\u4e2d\u4e3aclosesocket\u3002
\u3000\u3000Linux\u4e2d\u6709\u53d8\u91cfsocklen_t\uff0cWindows\u4e2d\u76f4\u63a5\u4e3aint\u3002
\u3000\u3000\u56e0\u4e3alinux\u4e2d\u7684socket\u4e0e\u666e\u901a\u7684fd\u4e00\u6837\uff0c\u6240\u4ee5\u53ef\u4ee5\u5728TCP\u7684socket\u4e2d\uff0c\u53d1\u9001\u4e0e\u63a5\u6536\u6570\u636e\u65f6\uff0c\u76f4\u63a5\u4f7f\u7528read\u548cwrite\u3002\u800cwindows\u53ea\u80fd\u4f7f\u7528recv\u548csend\u3002
\u3000\u3000\u8bbe\u7f6esocet\u9009\u9879\uff0c\u6bd4\u5982\u8bbe\u7f6esocket\u4e3a\u975e\u963b\u585e\u7684\u3002Linux\u4e0b\u4e3a
\u3000\u3000[cpp]
\u3000\u3000flag = fcntl (fd, F_GETFL);
\u3000\u3000fcntl (fd, F_SETFL, flag | O_NONBLOCK);
\u3000\u3000\uff0cWindows\u4e0b\u4e3a
\u3000\u3000[cpp]
\u3000\u3000flag = 1;
\u3000\u3000ioctlsocket (fd, FIONBIO, (unsigned long *) &flag);
\u3000\u3000\u3002
\u3000\u3000\u5f53\u975e\u963b\u585esocket\u7684TCP\u8fde\u63a5\u6b63\u5728\u8fdb\u884c\u65f6\uff0cLinux\u7684\u9519\u8bef\u53f7\u4e3aEINPROGRESS\uff0cWindows\u7684\u9519\u8bef\u53f7\u4e3aWSAEWOULDBLOCK\u3002
\u3000\u3000file
\u3000\u3000Linux\u4e0b\u9762\uff0c\u6587\u4ef6\u6362\u884c\u662f"\n"\uff0c\u800cwindows\u4e0b\u9762\u662f"\r\n"\u3002
\u3000\u3000Linux\u4e0b\u9762\uff0c\u76ee\u5f55\u5206\u9694\u7b26\u662f"/"\uff0c\u800cwindows\u4e0b\u9762\u662f"\"\u3002
\u3000\u3000Linux\u4e0eWindows\u4e0b\u9762\uff0c\u5747\u53ef\u4ee5\u4f7f\u7528stat\u8c03\u7528\u6765\u67e5\u8be2\u6587\u4ef6\u4fe1\u606f\u3002\u4f46\u662f\uff0cLinux\u53ea\u652f\u63012G\u5927\u5c0f\uff0c\u800cWindows\u53ea\u652f\u63014G\u5927\u5c0f\u3002\u4e3a\u4e86\u652f\u6301\u66f4\u5927\u7684\u6587\u4ef6\u67e5\u8be2\uff0c\u53ef\u4ee5\u5728Linux\u73af\u5883\u4e0b\u52a0
\u3000\u3000_FILE_OFFSET_BITS=64\u5b9a\u4e49\uff0c\u5728Windows\u4e0b\u9762\u4f7f\u7528_stat64\u8c03\u7528\uff0c\u5165\u53c2\u4e3astruct __stat64\u3002
\u3000\u3000Linux\u4e2d\u53ef\u6839\u636estat\u7684st_mode\u5224\u65ad\u6587\u4ef6\u7c7b\u578b\uff0c\u6709S_ISREG\u3001S_ISDIR\u7b49\u5b8f\u3002Windows\u4e2d\u6ca1\u6709\uff0c\u9700\u8981\u81ea\u5df1\u5b9a\u4e49\u76f8\u5e94\u7684\u5b8f\uff0c\u5982
\u3000\u3000[cpp]
\u3000\u3000#define S_ISREG(m) (((m) & 0170000) == (0100000))
\u3000\u3000#define S_ISDIR(m) (((m) & 0170000) == (0040000))
\u3000\u3000Linux\u4e2d\u5220\u9664\u6587\u4ef6\u662funlink\uff0cWindows\u4e2d\u4e3aDeleteFile\u3002
\u3000\u3000time
\u3000\u3000Linux\u4e2d\uff0ctime_t\u7ed3\u6784\u662f\u957f\u6574\u5f62\u3002\u800cwindows\u4e2d\uff0ctime_t\u7ed3\u6784\u662f64\u4f4d\u7684\u6574\u5f62\u3002\u5982\u679c\u8981\u5728windows\u59cbtime_t\u4e3a32\u4f4d\u65e0\u7b26\u53f7\u6574\u5f62\uff0c\u53ef\u4ee5\u52a0\u5b8f\u5b9a\u4e49\uff0c_USE_32BIT_TIME_T\u3002
\u3000\u3000Linux\u4e2d\uff0csleep\u7684\u5355\u4f4d\u4e3a\u79d2\u3002Windows\u4e2d\uff0cSleep\u7684\u5355\u4f4d\u4e3a\u6beb\u79d2\u3002\u5373\uff0cLinux\u4e0bsleep (1)\uff0c\u5728Windows\u73af\u5883\u4e0b\u5219\u9700\u8981Sleep (1000)\u3002
\u3000\u3000Windows\u4e2d\u7684timecmp\u5b8f\uff0c\u4e0d\u652f\u6301\u5927\u4e8e\u7b49\u4e8e\u6216\u8005\u5c0f\u4e8e\u7b49\u4e8e\u3002
\u3000\u3000Windows\u4e2d\u6ca1\u6709struct timeval\u7ed3\u6784\u7684\u52a0\u51cf\u5b8f\u53ef\u4ee5\u4f7f\u7528\uff0c\u9700\u8981\u624b\u52a8\u5b9a\u4e49\uff1a
\u3000\u3000[cpp]
\u3000\u3000#define MICROSECONDS (1000 * 1000)
\u3000\u3000
\u3000\u3000#define timeradd(t1, t2, t3) do { \
\u3000\u3000(t3)->tv_sec = (t1)->tv_sec + (t2)->tv_sec; \
\u3000\u3000(t3)->tv_usec = (t1)->tv_usec + (t2)->tv_usec % MICROSECONDS; \
\u3000\u3000if ((t1)->tv_usec + (t2)->tv_usec > MICROSECONDS) (t3)->tv_sec ++; \
\u3000\u3000} while (0)
\u3000\u3000
\u3000\u3000#define timersub(t1, t2, t3) do { \
\u3000\u3000(t3)->tv_sec = (t1)->tv_sec - (t2)->tv_sec; \
\u3000\u3000(t3)->tv_usec = (t1)->tv_usec - (t2)->tv_usec; \
\u3000\u3000if ((t1)->tv_usec - (t2)->tv_usec tv_usec --, (t3)->tv_usec += MICROSECONDS; \
\u3000\u3000} while (0)
\u3000\u3000\u8c03\u7528\u8fdb\u7a0b
\u3000\u3000Linux\u4e0b\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528system\u6765\u8c03\u7528\u5916\u90e8\u7a0b\u5e8f\u3002Windows\u6700\u597d\u4f7f\u7528WinExec\uff0c\u56e0\u4e3aWinExec\u53ef\u4ee5\u652f\u6301\u662f\u6253\u5f00\u8fd8\u662f\u9690\u85cf\u7a0b\u5e8f\u7a97\u53e3\u3002\u7528WinExec\u7684\u7b2c\u4e8c\u4e2a\u5165\u53c2\u6307\u660e\uff0c\u5982
\u3000\u3000SW_SHOW/SW_HIDE\u3002
\u3000\u3000\u6742\u9879
\u3000\u3000Linux\u4e3asrandom\u548crandom\u51fd\u6570\uff0cWindows\u4e3asrand\u548crand\u51fd\u6570\u3002
\u3000\u3000Linux\u4e3asnprintf\uff0cWindows\u4e3a_snprintf\u3002
\u3000\u3000\u540c\u7406\uff0cLinux\u4e2d\u7684strcasecmp\uff0cWindows\u4e3a_stricmp\u3002
\u3000\u3000\u9519\u8bef\u5904\u7406
\u3000\u3000Linux\u4e0b\u9762\uff0c\u901a\u5e38\u4f7f\u7528\u5168\u5c40\u53d8\u91cferrno\u6765\u8868\u793a\u51fd\u6570\u6267\u884c\u7684\u9519\u8bef\u53f7\u3002Windows\u4e0b\u8981\u4f7f\u7528GetLastError ()\u8c03\u7528\u6765\u53d6\u5f97\u3002
\u3000\u3000Linux\u73af\u5883\u4e0b\u4ec5\u6709\u7684
\u3000\u3000\u8fd9\u4e9b\u51fd\u6570\u6216\u8005\u5b8f\uff0cWindows\u4e2d\u5b8c\u5168\u6ca1\u6709\uff0c\u9700\u8981\u7528\u6237\u624b\u52a8\u5b9e\u73b0\u3002
\u3000\u3000atoll
\u3000\u3000[cpp]
\u3000\u3000long long
\u3000\u3000atoll (const char *p)
\u3000\u3000{
\u3000\u3000int minus = 0;
\u3000\u3000long long value = 0;
\u3000\u3000if (*p == '-')
\u3000\u3000{
\u3000\u3000minus ++;
\u3000\u3000p ++;
\u3000\u3000}
\u3000\u3000while (*p >= '0' && *p <= '9')
\u3000\u3000{
\u3000\u3000value *= 10;
\u3000\u3000value += *p - '0';
\u3000\u3000p ++;
\u3000\u3000}
\u3000\u3000return minus ? 0 - value : value;
\u3000\u3000}
\u3000\u3000gettimeofday
\u3000\u3000[cpp]
\u3000\u3000#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
\u3000\u3000#define EPOCHFILETIME 11644473600000000Ui64
\u3000\u3000#else
\u3000\u3000#define EPOCHFILETIME 11644473600000000ULL
\u3000\u3000#endif
\u3000\u3000
\u3000\u3000struct timezone
\u3000\u3000{
\u3000\u3000int tz_minuteswest;
\u3000\u3000int tz_dsttime;
\u3000\u3000};
\u3000\u3000
\u3000\u3000int
\u3000\u3000gettimeofday (struct timeval *tv, struct timezone *tz)
\u3000\u3000{
\u3000\u3000FILETIME ft;
\u3000\u3000LARGE_INTEGER li;
\u3000\u3000__int64 t;
\u3000\u3000static int tzflag;
\u3000\u3000
\u3000\u3000if (tv)
\u3000\u3000{
\u3000\u3000GetSystemTimeAsFileTime (&ft);
\u3000\u3000li.LowPart = ft.dwLowDateTime;
\u3000\u3000li.HighPart = ft.dwHighDateTime;
\u3000\u3000t = li.QuadPart; /* In 100-nanosecond intervals */
\u3000\u3000t -= EPOCHFILETIME; /* Offset to the Epoch time */
\u3000\u3000t /= 10; /* In microseconds */
\u3000\u3000tv->tv_sec = (long) (t / 1000000);
\u3000\u3000tv->tv_usec = (long) (t % 1000000);
\u3000\u3000}
\u3000\u3000
\u3000\u3000if (tz)
\u3000\u3000{
\u3000\u3000if (!tzflag)
\u3000\u3000{
\u3000\u3000_tzset ();
\u3000\u3000tzflag++;
\u3000\u3000}
\u3000\u3000tz->tz_minuteswest = _timezone / 60;
\u3000\u3000tz->tz_dsttime = _daylight;
\u3000\u3000}
\u3000\u3000
\u3000\u3000return 0;
\u3000\u3000}
\u3000\u3000\u7f16\u8bd1\u76f8\u5173
\u3000\u3000\u5f53\u524d\u51fd\u6570\uff0cLinux\u7528__FUNCTION__\u8868\u793a\uff0cWindows\u7528__func__\u8868\u793a\u3002
\u3000\u3000--------------------------------------------------------------------------------
\u3000\u3000Socket \u7f16\u7a0b windows\u5230Linux\u4ee3\u7801\u79fb\u690d\u9047\u5230\u7684\u95ee\u9898
\u3000\u30001)\u5934\u6587\u4ef6
\u3000\u3000windows\u4e0bwinsock.h/winsock2.h
\u3000\u3000linux\u4e0bsys/socket.h
\u3000\u3000\u9519\u8bef\u5904\u7406\uff1aerrno.h
\u3000\u30002)\u521d\u59cb\u5316
\u3000\u3000windows\u4e0b\u9700\u8981\u7528WSAStartup
\u3000\u3000linux\u4e0b\u4e0d\u9700\u8981
\u3000\u30003)\u5173\u95edsocket
\u3000\u3000windows\u4e0bclosesocket(...)
\u3000\u3000linux\u4e0bclose(...)
\u3000\u30004)\u7c7b\u578b
\u3000\u3000windows\u4e0bSOCKET
\u3000\u3000linux\u4e0bint
\u3000\u3000\u5982\u6211\u7528\u5230\u7684\u4e00\u4e9b\u5b8f\uff1a
\u3000\u3000#ifdef WIN32
\u3000\u3000typedef int socklen_t;
\u3000\u3000typedef int ssize_t;
\u3000\u3000#endif
\u3000\u3000#ifdef __LINUX__
\u3000\u3000typedef int SOCKET;
\u3000\u3000typedef unsigned char BYTE;
\u3000\u3000typedef unsigned long DWORD;
\u3000\u3000#define FALSE 0
\u3000\u3000#define SOCKET_ERROR (-1)
\u3000\u3000#endif
\u3000\u30005)\u83b7\u53d6\u9519\u8bef\u7801
\u3000\u3000windows\u4e0bgetlasterror()/WSAGetLastError()
\u3000\u3000linux\u4e0berrno\u53d8\u91cf
\u3000\u30006)\u8bbe\u7f6e\u975e\u963b\u585e
\u3000\u3000windows\u4e0bioctlsocket()
\u3000\u3000linux\u4e0bfcntl()
\u3000\u30007)send\u51fd\u6570\u6700\u540e\u4e00\u4e2a\u53c2\u6570
\u3000\u3000windows\u4e0b\u4e00\u822c\u8bbe\u7f6e\u4e3a0
\u3000\u3000linux\u4e0b\u6700\u597d\u8bbe\u7f6e\u4e3aMSG_NOSIGNAL\uff0c\u5982\u679c\u4e0d\u8bbe\u7f6e\uff0c\u5728\u53d1\u9001\u51fa\u9519\u540e\u6709\u53ef \u80fd\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u9000\u51fa\u3002
\u3000\u30008)\u6beb\u79d2\u7ea7\u65f6\u95f4\u83b7\u53d6
\u3000\u3000windows\u4e0bGetTickCount()
\u3000\u3000linux\u4e0bgettimeofday()
\u3000\u30003\u3001\u591a\u7ebf\u7a0b
\u3000\u3000\u591a\u7ebf\u7a0b: (win)process.h --\u3009(linux)pthread.h
\u3000\u3000_beginthread --> pthread_create
\u3000\u3000_endthread --> pthread_exit
\u3000\u3000-----------------------------------------------------------------
\u3000\u3000windows\u4e0elinux\u5e73\u53f0\u4f7f\u7528\u7684socket\u5747\u7ee7\u627f\u81eaBerkeley socket(rfc3493)\uff0c\u4ed6\u4eec\u90fd\u652f\u6301select I/O\u6a21\u578b\uff0c\u5747\u652f\u6301\u4f7f\u7528getaddrinfo\u4e0egetnameinfo\u5b9e\u73b0\u534f\u8bae\u65e0\u5173\u7f16\u7a0b\u3002\u4f46\u5b58\u5728\u7ec6\u5fae\u5dee\u522b\uff0c
\u3000\u3000\u4e3b\u8981\u6709\uff1a
\u3000\u3000\u5934\u6587\u4ef6\u53ca\u7c7b\u5e93\u3002windows\u4f7f\u7528winsock2.h(\u9700\u8981\u5728windows.h\u524d\u5305\u542b)\uff0c\u5e76\u8981\u94fe\u63a5\u5e93ws2_32.lib\uff1blinux\u4f7f\u7528netinet/in.h, netdb.h\u7b49\u3002
\u3000\u3000windows\u4e0b\u5728\u4f7f\u7528socket\u4e4b\u524d\u4e0e\u4e4b\u540e\u8981\u5206\u522b\u4f7f\u7528WSAStartup\u4e0eWSAClean\u3002
\u3000\u3000\u5173\u95edsocket\uff0cwindows\u4f7f\u7528closesocket\uff0clinux\u4f7f\u7528close\u3002
\u3000\u3000send*\u4e0erecv*\u51fd\u6570\u53c2\u6570\u4e4bsocket\u957f\u5ea6\u7684\u7c7b\u578b\uff0cwindows\u4e3aint\uff0clinux\u4e3asocklen_t\uff0c\u53ef\u9884\u7f16\u8bd1\u6307\u4ee4\u4e2d\u5904\u7406\u8fd9\u4e00\u5dee\u5f02\uff0c\u5f53\u5e73\u53f0\u4e3awindows\u65f6#define socklen_t unsigned int\u3002
\u3000\u3000select\u51fd\u6570\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0cwindows\u5ffd\u7565\u8be5\u53c2\u6570\uff0clinux\u4e0b\u8be5\u53c2\u6570\u8868\u793a\u96c6\u5408\u4e2dsocket\u7684\u4e0a\u9650\u503c\uff0c\u4e00\u822c\u8bbe\u4e3asockfd(\u9700select\u7684socket) + 1\u3002
\u3000\u3000windows\u4e0bsocket\u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578b\u4e3aSOCKET(unsigned int)\uff0c\u5176\u4e2d\u53d1\u751f\u9519\u8bef\u65f6\u8fd4\u56deINVALID_SOCKET(0)\uff0clinux\u4e0bsocket\u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578bint, \u53d1\u751f\u9519\u8bef\u65f6\u8fd4\u56de-1\u3002
\u3000\u3000\u53e6\u5916\uff0c\u5982\u679c\u7ed1\u5b9a\u672c\u673a\u56de\u73af\u5730\u5740\uff0cwindows\u4e0bsendto\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\uff0clinux\u4e0bsendto\u56de\u62a5\u9519\uff1aerrno=22, Invalid arguement\u3002\u4e00\u822c\u60c5\u51b5\u4e0b\u5747\u7ed1\u5b9a\u901a\u914d\u5730\u5740\u3002
\u8f6c\u8f7djlins
对,是那样的,用open打开文件,用read读取文件,在发送给对方,接收方接收到后,写入文件就可以了。不过在这个过程中最好别用字符串函数,除非你很熟悉。
第二个问题
首先你得去搞清楚什么是线程,什么是进程,fork出来的叫进程,pthread_create出来的才叫线程。服务器有很多种模型(多进程,多线程,select,epoll模型,这个我的blog上有,famdestiny.cublog.cn),不一定要用多进程。
给你写了个代码,自己先看看:
注意,在自己的目录下创建一个叫pserverb的文件,程序会把这个文件复制成test文件。你可以自己根据需要改改
server:
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define SERV_PORT 5358
#define MAX_CONN 10
#define BUF_LEN 1024
void str_echo(FILE *fp, int sockfd){
ssize_t nread;
int file_fd;
char buf[BUF_LEN] = {0};
file_fd = open("test", O_WRONLY | O_TRUNC | O_CREAT, 0755);
while(1) {
bzero(buf, BUF_LEN);
if((nread = read(sockfd, buf, BUF_LEN)) == -1) {
if(errno == EINTR) {
continue;
}
else {
printf("readn error: %s\n", strerror(errno));
continue;
}
}
else if (nread == 0) {
break;
}
else {
printf("%s\n", buf);
write(file_fd, buf, nread);
}
}
close(file_fd);
}
void sig_chld(int sig){
pid_t pid;
int state;
while((pid = waitpid(-1, &state, WNOHANG)) > 0){
printf("child process %d exited.", pid);
}
return;
}
int main(int argc, char **argv)
{
int listenfd, connfd;
socklen_t cliaddrlen;
pid_t childpid;
struct sockaddr_in servaddr, cliaddr;
if((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
printf("socket error: %s\n", strerror(errno));
return 0;
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);
if(bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1){
printf("bind error: %s\n", strerror(errno));
return 0;
}
if(listen(listenfd, MAX_CONN) == -1){
printf("listen error: %s\n", strerror(errno));
return 0;
}
signal(SIGCHLD, sig_chld);
while(1){
cliaddrlen = sizeof(cliaddr);
if((connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &cliaddrlen)) == -1){
if(errno == EINTR){
continue;
}
else{
printf("accept error: %s\n", strerror(errno));
continue;
}
}
if((childpid = fork()) == 0){
close(listenfd);
str_echo(stdin, connfd);
exit(0);
}
else if(childpid > 0){
close(connfd);
}
else{
printf("fork error!\n");
continue;
}
}
}
client:
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#define SERV_ADDR "127.0.0.1"
#define SERV_PORT 5358
#define BUF_LEN 1024
void str_cli(char *path, int sockfd)
{
char sendbuf[BUF_LEN] = {0};
int fd, n;
if((fd = open("./pserverb", O_RDONLY)) == -1){
printf("%s\n", strerror(errno));
exit(0);
}
while((n = read(fd, sendbuf, BUF_LEN)) != 0) {
if(n < 0){
printf("%s\n", strerror(errno));
exit(0);
}
write(sockfd, sendbuf, n);
bzero(sendbuf, BUF_LEN);
}
close(fd);
return;
}
int main(int argc, char **argv)
{
int fd;
struct sockaddr_in servaddr;
fd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr(SERV_ADDR);
servaddr.sin_port = htons(SERV_PORT);
if (connect(fd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1) {
printf("connect error: %s\n", strerror(errno));
return 0;
}
str_cli(argv[1], fd);
return 0;
}
1你就打开文件,然后一个字符一个字符的读文件,然后一个字符一个字符的接收,再一个字符一个字符写入文件就行了阿
2这个问题你问的有点模糊,服务器端的编程有很多方式,也不是要每来个连接就要创建一个进程的。
挖~
同烂
扩展阅读:xbox series x ... kali linux手机直装版 ... java blockingqueue ... java windowbuilder ... matebook macbook ... macbookpro ... matebook14 ... xboxone欧版 ... javascript入门 ...