최대 1 분 소요

어디에서 어디를 호출할지. ^^; 그림 좀 그려보고 짜보자구

server - link link2 (enrol_method) (nonblocking_mode)


(noblocking server side code)\

  1. #include
  2. #include
  3. #include <sys/socket.h>
  4. #include
  5. #include <netinet/in.h>
  6. #include
  7. #include
  8. #define PORT 8080
  9. int main(int argc, char const *argv[])
  10. {
  11. int server_fd, new_socket, valread;
  12. struct sockaddr_in address;
  13. int opt = 1;
  14. int addrlen = sizeof(address);
  15. char buffer[1024] = {0};
  16. char *hello = ”Hello from server”;
  17. //Creating socket file descriptor
  18. if((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0 )
  19. {
  20. perror(“socket failed”);
  21. exit(EXIT_FAILURE);
  22. }
  23. if(setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR   SO_REUSEPORT, & opt, sizeof(opt)))
  24. {
  25. perror(“setsockopt”);
  26. exit(EXIT_FAILURE);
  27. }
  28. address.sin_family = AF_INET;
  29. address.sin_addr.s_addr = INADDR_ANY;
  30. address.sin_port = htons(PORT);
  31. if(bind(server_fd, (struct sockaddr *)&address, sizeof(address))<0)
  32. {
  33. perror(“bind failed”);
  34. exit(EXIT_FAILURE);
  35. }
  36. if(listen(server_fd, 3)< 0)
  37. {
  38. perror(“listen”);
  39. exit(EXIT_FAILURE);
  40. }
  41. if((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0)
  42. {
  43. printf(“server accept failed….\n”);
  44. exit(0);
  45. }
  46. int flag = fcntl(new_socket, F_GETFL, 0);
  47. fcntl(new_socket, F_SETFL, flag   O_NONBLOCK);
  48. int read_size = 0;
  49. while(1)
  50. {
  51. read_size = read(new_socket, buffer, 1024);
  52. if(read_size > 0 )
  53. {
  54. printf(“%s\n”,buffer);
  55. write(new_socket, hello, strlen(hello));
  56. printf(“Hello message sent\n”);
  57. }
  58. }
  59. return 0;
  60. }

client winform c# - link


  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Net;
  11. using System.Net.Sockets;
  12. using SimpleTCP;
  13. namespace tcp_ip_client_test
  14. {
  15. public partial class Form1 : Form
  16. {
  17. public Form1()
  18. {
  19. InitializeComponent();
  20. }
  21. SimpleTcpClient client;
  22. private void button1_Click(object sender, EventArgs e)  //connect
  23. {
  24. connectBtn.Enabled = false;
  25. //Connect to server
  26. client.Connect(ttxtHost.Text, Convert.ToInt32(txtPort.Text));
  27. }
  28. private void Form1_Load(object sender, EventArgs e)
  29. {
  30. client = new SimpleTcpClient();
  31. client.StringEncoder = Encoding.UTF8;
  32. client.DataReceived += Client_DataReceived;
  33. }
  34. private void Client_DataReceived(object sender, SimpleTCP.Message e)
  35. {
  36. //Update message to txtStatus
  37. txtStatus.Invoke((MethodInvoker)delegate ()
  38. {
  39. txtStatus.Text += e.MessageString;
  40. });
  41. }
  42. private void button2_Click(object sender, EventArgs e)  //send
  43. {
  44. client.WriteLineAndGetReply(txtMessage.Text, TimeSpan.FromSeconds(3));
  45. }
  46. }
  47. }

socket_flow socket_flow_2

link1 link1-1 link1-2

link2

fixed ip setting guide