socket请求怎么写( 四 )

#define SERVER_PORT 5208 //侦听端口 void main() { WORD wVersionRequested; WSADATA wsaData; int ret; SOCKET sClient; //连接套接字 struct sockaddr_in saServer; //地址信息 char *ptr; BOOL fSuccess = TRUE;//WinSock初始化 wVersionRequested = MAKEWORD(2, 2); //希望使用的WinSock DLL的版本 ret = WSAStartup(wVersionRequested, &wsaData); if(ret!=0) { printf("WSAStartup() failed!\n"); return; }//确认WinSock DLL支持版本2.2 if(LOBYTE(wsaData.wVersion)!=2 || HIBYTE(wsaData.wVersion)!=2) { WSACleanup(); printf("Invalid WinSock version!\n"); return; }//创建Socket,使用TCP协议 sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (sClient == INVALID_SOCKET) { WSACleanup(); printf("socket() failed!\n"); return; }//构建服务器地址信息 saServer.sin_family = AF_INET; //地址家族 saServer.sin_port = htons(SERVER_PORT); //注意转化为网络节序 saServer.sin_addr.S_un.S_addr = inet_addr("192.168.1.127");//连接服务器 ret = connect(sClient, (struct sockaddr *)&saServer, sizeof(saServer)); if (ret == SOCKET_ERROR) { printf("connect() failed!\n"); closesocket(sClient); //关闭套接字 WSACleanup(); return; } char sendMessage[]="hello this is client message!"; ret = send (sClient, (char *)&sendMessage, sizeof(sendMessage), 0); if (ret == SOCKET_ERROR) { printf("send() failed!\n"); } else printf("client info 。
9.请问Socket编程的基本步骤是怎样的SERVER端:--------------------------------------------------------import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;public class Server extends Thread { private Socket clientSocket; public Server(Socket clientSocket) { this.clientSocket = clientSocket; } public void run() { DataInputStream dis = null; DataOutputStream dos = null; try { dis = new DataInputStream(clientSocket.getInputStream()); dos = new DataOutputStream(clientSocket.getOutputStream()); while (true) { String temp = dis.readUTF(); if ("over".equals(temp)) { break; } dos.writeUTF("from server:" + temp); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (dis != null) { dis.close(); } if (dis != null) { dos.close(); } if (clientSocket != null) { clientSocket.close(); } } catch (IOException e) { } } } public static void main(String[] args) throws Exception { ServerSocket ss = new ServerSocket(8008); while (true) { Socket clientSocket = ss.accept(); // 针对每个客户端,启一个Server线程专门处理此客户端的请求 。
Server server = new Server(clientSocket); server.start(); } }}CLIENT端:----------------------------------------import java.io.BufferedReader;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.InputStreamReader;import java.net.Socket;public class Client { public static void main(String[] args) throws Exception { // 输入流1,从键盘进入Client 。InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); Socket clientSocket = new Socket("127.0.0.1", 8008); // 输入流2,从服务器端进入Client的流对象 。
DataInputStream dis = new DataInputStream(clientSocket.getInputStream()); // 输出流,从Client出去,到服务器端 。DataOutputStream dos = new DataOutputStream(clientSocket.getOutputStream()); while (true) { // 从键盘输入读取 String msg = br.readLine(); // 将读取信息发送给服务器端 dos.writeUTF(msg); //输入QUIT退出 if ("QUIT".equals(msg)) { break; } //读取从服务器返回的信息 String temp = dis.readUTF(); System.out.println(temp); } br.close(); dis.close(); dos.close(); clientSocket.close(); }} 。
【socket请求怎么写】

socket请求怎么写

文章插图